为什么我在Ruby on Rails应用程序中遇到未定义的方法错误?

时间:2022-06-01 19:57:28

Thanks for any help you can offer. I'm trying to figure out why I'm getting the undefined method error described below. I'm new to Ruby on Rails, but I thought that by creating the @waypoint, I could call methods for it (like @waypoint.waypointaddress). My models use a belongs_to and has many (Newsavedmaps can have many waypoints.) I appreciate your feedback on what I'm doing wrong.

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

Error Refers to if !@waypoint.waypointaddress.nil?

错误是指如果!@waypoint.waypointaddress.nil?

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

maptry controller

测绘控制器

    @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

EDIT 1

编辑1

Here is my updated controller using code from Mu's comment. However, I'm getting an error of "undefined method `each'" for the maptry.html.erb view included below.

这是我使用Mu评论代码更新的控制器。但是,对于下面包含的maptry.html.erb视图,我收到“undefined method` each'”的错误。

To clarify, there may be one or more waypoints that match the conditions. I'd like to return all of them and then do a for each in my maptry view.

为了澄清,可能存在一个或多个符合条件的航路点。我想返回所有这些,然后在我的maptry视图中为每个做一个。

Also, the .where(...) threw an undefined method error for where, so I used find. Maybe that's related to the problem? I'm in Rails 2.X.

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

Code from maptry.html.erb

来自maptry.html.erb的代码

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

updated maptry controller

更新的地图控制器

    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

1 个解决方案

#1


3  

You're asking ActiveRecord to find all the waypoints that match your conditions:

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

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

That means that you'll get an Array in @waypoint and an Array (usually) won't have a waypointaddress method. Perhaps you meant to ask for the first one:

这意味着你将在@waypoint中获得一个数组,而一个数组(通常)将没有一个waypointaddress方法。也许你打算要求第一个:

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

or better:

或更好:

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

In general, you shouldn't be using find like that anymore; if you find yourself entering some :conditions in a find call, you should be saying .where(...) instead.

一般来说,你不应该再使用find了;如果你发现自己输入了一些:发现呼叫的条件,你应该说.where(...)代替。

#1


3  

You're asking ActiveRecord to find all the waypoints that match your conditions:

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

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

That means that you'll get an Array in @waypoint and an Array (usually) won't have a waypointaddress method. Perhaps you meant to ask for the first one:

这意味着你将在@waypoint中获得一个数组,而一个数组(通常)将没有一个waypointaddress方法。也许你打算要求第一个:

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

or better:

或更好:

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

In general, you shouldn't be using find like that anymore; if you find yourself entering some :conditions in a find call, you should be saying .where(...) instead.

一般来说,你不应该再使用find了;如果你发现自己输入了一些:发现呼叫的条件,你应该说.where(...)代替。