1

在我的 Rails 应用程序中,我分别有两个控制器坐标和推文及其表。我的搜索按钮中有两个文本字段,我需要使用坐标表中的条件从推文表中获取查询。但我收到此错误 "undefined method `parameters' for nil:NilClass" 。我的坐标控制器.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 %>

我的查看代码以显示推文

%= 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>

我坚持了很长时间,任何人都请帮助我继续。提前谢谢

4

1 回答 1

1

建议:安装 dev gem better_errors:https ://github.com/charliesome/better_errors

有了它,当您的代码失败时,您将在浏览器中获得一个不错的控制台。因此,如果我在您所在的位置,我会插入一些虚假代码:

#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

为确保它在此行之后失败,然后在控制台中检查 @tweets 等是否实际上是对象等等。重复可能导致此问题的每一行关键代码。

于 2013-07-17T14:17:34.037 回答