user2492854
user2492854

Reputation: 401

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

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

Upvotes: 2

Views: 2885

Answers (1)

Svennisen
Svennisen

Reputation: 194

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.

Upvotes: 1

Related Questions