undefined method `parameters' for nil:NilClass in rails 3 - ruby-on-rails

in my rails app i have two controllers coordinates and tweets and their tables respectively.i have two text fields in my search button and i need to fetch queries from tweets table using the conditions in coordinates table . But I am getting this error "undefined method `parameters' for nil:NilClass" .
My coordinates_controller.rb
class CoordinatesController<ApplicationController
def paramas(b)
#b = params[:show]
return #b
end
def coor(latitude,longitude)
#latitude=0`enter code here`
#longitude=0
end
def search
#a=Coordinates.where(city: params[:show]).take
if(params[:show]== a.city) then
#latitude= a.latitude
#longitude=a.longitude
end
if(#latitude=0 && #longitude=0) then
return #sql="Select * from tweets where tweet_text LIKE '%search%' AND user_loc LIKE 'a.paramas' order by id desc"
else if (#latitude!=0 && #longitude!=0)
#min_lat = #latitude - 1.0
#max_lat = #latitude + 1.0
#min_lng = #longitude - 1.0
#max_lng = #longitude + 1.0
return #sql = "Select * from tweets where tweet_text LIKE '%search%' AND ( ((longitude BETWEEN min_lng and max_lng) AND (latitude BETWEEN min_lat and max_lat)) OR (user_loc LIKE 'a.paramas') ) order by id desc"
else
return #sql="Select * from tweets where tweet_text LIKE '%search%'"
end
end
end
# a= CoordinatesController.new
end
class TweetsController<ApplicationController
require 'coordinates_controller.rb'
#require 'models/coordinates.rb'
def index
#include 'coordinates_controller.rb'
a= CoordinatesController.new
#sql=a.search
#tweets=Tweets.paginate_by_sql(sql, :#page, :per_page => #per_page ).all
end
end
My view code for the search button
<%= form_tag({controller: "tweets", action:"index" }, method: "get") do %>
<%= label_tag(:search, "search for:") %>
<%= text_field_tag(:search) %>
<%= text_field_tag(:show) %>
<%= submit_tag("get results ") %>
<% end %>
My view code to display tweets
%= will_paginate #tweets %>
<% #tweets.each do |tweets| %>
<ul>
<li><%= tweets.id %></li>
<li><%= tweets.tweet_created_at %></li>
<li><%= tweets.tweet_id %></li>
<li><%= tweets.tweet_source %></li>
<li><%= tweets.tweet_text %></li>
<li><%= tweets.user_id %></li>
<li><%= tweets.user_name %></li>
<li><%= tweets.user_sc_name %></li>
<li><%= tweets.user_loc %></li>
<li><%= tweets.user_img %></li>
<li><%= tweets.longitude %></li>
<li><%= tweets.latitude %></li>
<li><%= tweets.place %></li>
<li><%= tweets.country %></li>
<% end %>
</ul>
Iam stuck up with this for a long time anybody pls help me to proceed. thks in advance

Suggestion:
Install dev gem better_errors: https://github.com/charliesome/better_errors
With it you will get a nice console in your browser when your code fails.
So if I where you, I would insert some bogus code:
#include 'coordinates_controller.rb'
a= CoordinatesController.new
#sql=a.search
#tweets=Tweets.paginate_by_sql(sql, :#page, :per_page => #per_page ).all
likethisboguscodeBLABLABLA
end
end
To make sure it fails after this line, then in the console, check that #tweets etc is actually objects and so on. Repeat on every crucial line of code that could cause this problem.

Related

How to group some values rails

I'm using rails 4. And i'm trying to output menu navigation. I got tble PlaceType with category and title columns. There are 6 categories and i want to output Category only 1 time and then output all titles, that brlong to this ategory. How can i achieve that?
<% #types = PlaceType.all %>
<% #types.each do |type| %>
<ul><%= type.category %>
<li><%= type.title %></li>
</ul>
<% end %>
That's my way of sloving this problem:
<% #types = PlaceType.all %>
<% #types.group_by(&:category).each do |category, type| %>
<ul><%= category %>
<% type.each do |t|%>
<li><%= t.title %></li>
<% end %>
</ul>
<% end %>
Use the group_by method available in Array.
#types.group_by {|type| type.category}
Will give you a hash that looks something like this:
{
"Category A" => ["Title 1", "Title 2"],
"Category B" => ["Title 3"],
"Category C" => ["Title 4", "Title 5"]
}
Now you can loop through the resulting hash instead, to display titles under each category.

What is the proper way to form this multi-dimensional array?

I am attempting to perform a series of requests that will organize the results in a given date span in a way where each location will live on the top level, and contain an another array with each set of results. Here is what I have so far:
locations = Location.all
#requests = []
locations.each do |location|
request = PurchaseRequest.where('created_at >= ? AND created_at <= ? AND location_id = ?', params[:start_date], params[:end_date], location.id).order(:location_id)
#requests.push(location => request)
end
My ideal (non-working) implementation in the view would look something like:
<ul>
<% #requests.each do |location| %>
<li>location[0].name</li>
<ul>
<% location[1].each do |request| %>
<li>request.name</li>
<% end %>
</ul>
<% end %>
</ul>
Try something like this:
#requests = {}
Location.all.each do |location|
#requests[location.name] = PurchaseRequest.where('created_at >= ? AND created_at <= ? AND location_id = ?', params[:start_date], params[:end_date], location.id).order(:location_id)
end
and:
<ul>
<% #requests.each do |location_name, request_list| %>
<li><%= location_name %></li>
<ul>
<% request_list.each do |request| %>
<li><%= request.name %></li>
<% end %>
</ul>
<% end %>
</ul>
#requests becomes a hash with the key being the location name and the value being the purchase requests.

how to define methods that use the variables of some other controller in rails 3

In My rails application I have two controllers namely tweets_controller and coordinates_controller.In my final view I want to display the attributes of tweets_controller using the condition in coordinates_controller. The problem I am facing is that tweets_controller should know the variables of coordinates_controller.
my code for coordinates_controller
def CoordinatesHelper.query()
a = Coordinates.where(city: params[:show])
b = a.first
if a.count == 1
latitude = b.latitude
longitude= b.longitude
end
if(latitude=0 && longitude=0)
sql="Select * from tweets where tweet_text LIKE '%text%' AND user_loc LIKE 'show' order by id desc LIMIT 30"
else if (latitude!=0 && longitude!=0)
min_lat = latitude - 1.0
max_lat = latitude + 1.0
min_lng = longitude - 1.0
max_lng = longitude + 1.0
sql = "Select * from tweets where tweet_text LIKE '%text%' AND ( ((longitude BETWEEN min_lng and max_lng) AND (latitude BETWEEN min_lat and max_lat)) OR (user_loc LIKE 'show') ) order by id desc LIMIT 30"
else
sql="Select * from tweets where tweet_text LIKE '%text%' LIMIT 30"
end
end
end
My tweets_controller just displays the attributes of the tweets table .using the definition 'index' in the tweets_controller.The conditions such as latitude,longitude comes from coordinates table, is there any way by which the tweets_controller will be able to evaluate the coordinates table latitude, longitude and then display the tweets in the final view. To be precise the coordinates_controller has variables latitude,longitude and city,and params[:show] in the search button. The tweets_controller should have access to the attributes of coordinates_controller. How should I do that?
<%= #tweets.each do |tweets| %>
<ul>
<li><%= tweets.id %></li>
<li><%= tweets.tweet_created_at %></li>
<li><%= tweets.tweet_source %></li>
<li><%= tweets.tweet_text %></li>
<li><%= tweets.user_id %></li>
<li><%= tweets.user_name %></li>
<li><%= tweets.user_sc_name %></li>
<li><%= tweets.user_loc %></li>
<li><%= tweets.user_img %></li>
<li><%= tweets.longitude %></li>
<li><%= tweets.latitude %></li>
<li><%= tweets.place %></li>
<li><%= tweets.country %></li>
</ul>
<% end %>
This is the final view content and to display this, the conditions of latitude and longitude are in coordinates table
What you need here is an association at the model level in between the Tweet model and Coordinate model, like this:
class Tweet < ActiveRecord::Base
has_one :coordinate
end
this tells that a tweet have coordinate.
the problem is not about accessing variables from one controller to another(that should never happen unless it is parent-child controllers)
but the problem is that you dont associations between models properly setup.

rails showing blank page

In my rails app I need to implement a conditional search form .There are two tables named tweets and coordinates , but the output is showing a blank page and I am stuck up with dis.
my coordinates_controller
class CoordinatesController < ApplicationController
def home
end
# def paramas(b)
#
# #b = params[:show]
# return #b
end
#def coor(latitude,longitude)
# #latitude=0
##longitude=0
#end
def query
#a=Coordinates.find("city=?", params[:show])
if(params[:show]= a.city) then
latitude= a.latitude
longitude=a.longitude
end
if(latitude=0 && longitude=0) then
return sql="Select * from tweets where tweet_text LIKE '%text%' AND user_loc LIKE 'params[:show]' order by id desc"
else if (latitude!=0 && longitude!=0)
min_lat = latitude - 1.0
max_lat = latitude + 1.0
min_lng = longitude - 1.0
max_lng = longitude + 1.0
return sql = "Select * from tweets where tweet_text LIKE '%text%' AND ( ((longitude BETWEEN min_lng and max_lng) AND (latitude BETWEEN min_lat and max_lat)) OR (user_loc LIKE 'params[:show]') ) order by id desc"
else
return sql="Select * from tweets where tweet_text LIKE '%text%'"
end
end
end
My Tweets_controller
class TweetsController < ApplicationController
include CoordinatesHelper
def search
render 'tweets/search'
end
def index
# include CoordinatesHelper
sql=query
#tweets=Tweets.paginate_by_sql(sql, :page => #page, :per_page => #per_page )
#render 'tweets/index'
end
end
my view code for the search button
<%=form_tag({controller: 'tweets', action:'index'}, method: "get") do %>
<%=label_tag(:search, "Search for:") %>
<%=text_field_tag(:text) %>
<%=label_tag(:show, "Show for:") %>
<%=text_field_tag(:show) %>
<%= submit_tag( "GO" ) %>
<% end %>
my view code to display the results
<%= will_paginate #tweets %>
<% #tweets.each do |tweets| %>
<ul>
<li><%= tweets.id %></li>
<li><%= tweets.tweet_created_at %></li>
<li><%= tweets.tweet_source %></li>
<li><%= tweets.tweet_text %></li>
<li><%= tweets.user_id %></li>
<li><%= tweets.user_name %></li>
<li><%= tweets.user_sc_name %></li>
<li><%= tweets.user_loc %></li>
<li><%= tweets.user_img %></li>
<li><%= tweets.longitude %></li>
<li><%= tweets.latitude %></li>
<li><%= tweets.place %></li>
<li><%= tweets.country %></li>
</ul>
<% end %>
But for some reasons the output is showing an empty page tried editing several tymes but in vain. Anybody kindly help me wid dis, I am struggling wid dis from a long tym
In your query method, you wrote and finder method, but you have write table name there, not model name, finders works on Model name, write like this :
In your query method replace this :
#a=Coordinates.find("city=?", params[:show])
with this :
#a=Coordinate.find("city=?", params[:show])
because model name, always remain singular, unless you make changes any configuration.
and I think, there is a problem with your params[:show] with this. Please paste your result of 'params[:show]'
Hope it will help. Thanks

rails 3 'undefined method' when a method is called inside another method

I have two tables called coordinates and tweets in my database, and I need to perform a search to fetch the tweets from the tweets table. Since this also requires data from coordinates table, I decided to use require and then create an object and then call the function of the other program inside the other program.
My coordinates_controller:
class CoordinatesController<ApplicationController
def paramas(b)
#b = params[:show]
return #b
end
def coor(latitude,longitude)
#latitude=0
#longitude=0
end
def search(min_latitude,max_latitude,min_longitude,max_longitude,sql)
#a = Coordinates.where(city :params[:show] ).take
if(params[:show]== a.city) then
#latitude= a.latitude
#longitude=a.longitude
end
if(#latitude=0 && #longitude=0) then
return #sql="Select * from tweets where tweet_text LIKE '%search%' AND user_loc LIKE 'a.paramas' order by id desc"
else if (#latitude!=0 && #longitude!=0)
#min_lat = #latitude - 1.0
#max_lat = #latitude + 1.0
#min_lng = #longitude - 1.0
#max_lng = #longitude + 1.0
return #sql = "Select * from tweets where tweet_text LIKE '%search%' AND ( ((longitude BETWEEN min_lng and max_lng) AND (latitude BETWEEN min_lat and max_lat)) OR (user_loc LIKE 'a.paramas') ) order by id desc"
else
return #sql="Select * from tweets where tweet_text LIKE '%search%'"
end
end
end
# a= CoordinatesController.new
end
My tweets_controller
require 'coordinates_controller.rb'
#require 'models/coordinates.rb'
class TweetsController<ApplicationController
def show
#include 'coordinates_controller.rb'
a= CoordinatesController.new
#sql=a.search(min_latitude,max_latitude,min_longitude,max_longitude,sql)
#tweets=Tweets.paginate_by_sql(sql, :#page, :per_page => #per_page ).all
end
end
my view code for the search button
<%= form_tag({controller: "tweets", action:"show" }, method: "get") do %>
<%= label_tag(:search, "search for:") %>
<%= text_field_tag(:show) %>
<%= submit_tag("Search") %>
<% end %>
<%= form_tag({controller: "coordinates", action:"search" }, method: "get") do %>
<%= label_tag(:search, "search for:") %>
<%= text_field_tag(:search) %>
<%= submit_tag("Search") %>
<% end %>
My view code for displaying results
<%= will_paginate #tweets %>
<% #tweets.each do |tweets| %>
<ul>
<li><%= tweets.id %></li>
<li><%= tweets.tweet_created_at %></li>
<li><%= tweets.tweet_id %></li>
<li><%= tweets.tweet_source %></li>
<li><%= tweets.tweet_text %></li>
<li><%= tweets.user_id %></li>
<li><%= tweets.user_name %></li>
<li><%= tweets.user_sc_name %></li>
<li><%= tweets.user_loc %></li>
<li><%= tweets.user_img %></li>
<li><%= tweets.longitude %></li>
<li><%= tweets.latitude %></li>
<li><%= tweets.place %></li>
<li><%= tweets.country %></li>
<% end %>
</ul>
I am getting the error undefined method min_latitude. I can't figure out what is causing the error.
When you call your method search Ruby don't know what min_latitude means here
#sql=a.search(min_latitude,max_latitude,min_longitude,max_longitude,sql)

Resources