0

谢谢你的尽心帮助。我试图弄清楚为什么会出现下面描述的未定义方法错误。我是 Ruby on Rails 的新手,但我认为通过创建@waypoint,我可以为它调用方法(如@waypoint.waypointaddress)。我的模型使用belongs_to 并且有很多(Newsavedmaps 可以有很多航路点。)感谢您对我做错的地方的反馈。

错误 指 if !@waypoint.waypointaddress.nil?

    NoMethodError (undefined method `waypointaddress' for #<Array:NUMBERSHERE>):
    app/controllers/maptry_controller.rb:33:in `index'

地图控制器

    @newsavedmap = Newsavedmap.find(:first, :conditions => {:id => params[:newsavedmap_id]})
    @waypoint = Waypoint.find(:all, :conditions => {:newsavedmap_id => params[:newsavedmap_id]})
    @newsavedmap.id = params[:newsavedmap_id]
    @newsavedmap.name = Newsavedmap.find(:first, :conditions => {:id => params[:newsavedmap_id]}).name
    @newsavedmap.optimize = Newsavedmap.find(:first, :conditions => {:id => params[:newsavedmap_id]}).optimize

    if !@newsavedmap.start_masterlocation_id.nil?       
    @start_i_name = Masterlocation.find(:first, :conditions => {:id => @newsavedmap.start_masterlocation_id}).i_name
    end
    if !@waypoint.waypointaddress.nil?  
    @waypoint_i_name = Masterlocation.find(:first, :conditions => {:id => @waypoint.waypoint_masterlocation_id}).i_name
    end
    if !@newsavedmap.end_masterlocation_id.nil?     
    @end_i_name = Masterlocation.find(:first, :conditions => {:id => @newsavedmap.end_masterlocation_id}).i_name
    end

    else
        @newsavedmap = Newsavedmap.new  
    end

编辑 1

这是我使用 Mu 评论中的代码更新的控制器。但是,对于下面包含的 maptry.html.erb 视图,我收到“未定义方法‘每个’”的错误。

澄清一下,可能有一个或多个符合条件的航路点。我想退回所有这些,然后for each在我的地图视图中做一个。

另外,.where(...)抛出了一个未定义的方法错误where,所以我使用了find. 也许这与问题有关?我在 Rails 2.X 中。

来自 maptry.html.erb 的代码

    <% if params[:newsavedmap_id] %>
            <% for waypoint in @waypoint %>
          <option selected value="<%= @waypoint.waypointaddress %>"><%= @waypoint_inst_name %></option>
            <% end %>
          <% end %>

更新的地图控制器

    if params[:newsavedmap_id]
    @newsavedmap = Newsavedmap.find(:first, :conditions => {:id => params[:newsavedmap_id]})
    @waypoint = Waypoint.find(:first, :conditions => {:newsavedmap_id => params[:newsavedmap_id]})
    @waypoint.waypointaddress = Waypoint.find(:first, :conditions => {:newsavedmap_id => params[:newsavedmap_id]}).waypointaddress
    @waypoint.waypoint_masterlocation_id = Waypoint.find(:first, :conditions => {:newsavedmap_id => params[:newsavedmap_id]}).waypoint_masterlocation_id
    @waypoint_inst_name = Masterlocation.find(:first, :conditions => {:id => @waypoint.waypoint_masterlocation_id}).inst_name
    @newsavedmap.id = params[:newsavedmap_id]
    @newsavedmap.name = Newsavedmap.find(:first, :conditions => {:id => params[:newsavedmap_id]}).name
    @newsavedmap.optimize = Newsavedmap.find(:first, :conditions => {:id => params[:newsavedmap_id]}).optimize

    if !@newsavedmap.start_masterlocation_id.nil?       
    @start_inst_name = Masterlocation.find(:first, :conditions => {:id => @newsavedmap.start_masterlocation_id}).inst_name
    end
    if !@newsavedmap.end_masterlocation_id.nil?     
    @end_inst_name = Masterlocation.find(:first, :conditions => {:id => @newsavedmap.end_masterlocation_id}).inst_name
    end

    else
    @newsavedmap = Newsavedmap.new  
    end
4

1 回答 1

3

您要求 ActiveRecord 找到所有符合您条件的航路点:

@waypoint = Waypoint.find(:all, :conditions => {:newsavedmap_id => params[:newsavedmap_id]})
#-------------------------^^^^

这意味着您将获得一个数组,@waypoint而一个数组(通常)将没有waypointaddress方法。也许你想问一个:

@waypoint = Waypoint.find(:first, :conditions => {:newsavedmap_id => params[:newsavedmap_id]})

或更好:

@waypoint = Waypoint.where(:newsavedmap_id => params[:newsavedmap_id]).first

一般来说,你不应该再这样使用find了。如果您发现自己:conditionsfind通话中输入了一些内容,则应该.where(...)改为说。

于 2013-08-17T01:57:03.887 回答