2013-07-29 44 views
0

在我的Rails应用程序中,我有两个名为coordinate和tweets的表。要提取查询,条件在坐标控制器中决定并在tweets控制器中执行。它基本上是一个tweets搜索表来获取匹配的推文。 。我正在使用find_by_sql方法。 我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.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变量来自coordinates_controller,并通过微博controller.But决定因某些原因tweets_controller不识别坐标的控制器内的SQL变量。它说“未定义的方法或局部变量sql”。任何帮助表示赞赏

+2

我投下了这个,因为很明显你不太了解Rails。如果您还没有在线教程,我建议您通过其中一个在线教程。 – dpassage

回答

1

CoordinatesController#query定义中的局部变量sql在定义以外的任何地方都不可见。即使您将其显示为CoordinatesController(通过将其转换为实例变量)的实例,它也不可见于TweetsController

+0

我应该怎么做如果必须让它在推文控制器内部可访问? – user2492854

+0

这取决于两个类之间的关系。我认为在继续使用Rails项目之前,您应该学习Ruby的基础知识。 – sawa

相关问题