0

在我的 rails 应用程序中,我有两个名为坐标和推文的表。要获取查询,条件在坐标控制器中决定并在推文控制器中执行。它基本上是一个推文搜索表来获取匹配的推文。. 我正在使用 find_by_sql 方法。我的坐标控制器

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.where(city: params[:show])
    b = a.first
    if a.count == 1
      latitude = b.latitude
      longitude= b.longitude
    end

    if(latitude=0 && longitude=0) then
      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     
  #end


  #end

我的 tweets_controller

class TweetsController < ApplicationController

  include CoordinatesHelper
  def search
    render 'tweets/search'
  end


  def index

    # include CoordinatesHelper
   # sql=query
    @tweets=Tweets.find_by_sql(sql)
    #render 'tweets/index'
  end
end

sql 变量来自坐标控制器,由 tweets 控制器决定。但由于某些原因,tweets_controller 无法识别坐标控制器内的 sql 变量。它说“未定义的方法或局部变量 sql”。任何帮助表示赞赏

4

1 回答 1

1

定义中的局部变量sqlCoordinatesController#query定义之外的任何地方都不可见。即使您使其对 的实例可见CoordinatesController(通过将其转换为实例变量),它对TweetsController.

于 2013-07-29T05:13:16.637 回答