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.
Error Refers to if [email protected]?
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 [email protected]_masterlocation_id.nil?
@start_i_name = Masterlocation.find(:first, :conditions => {:id => @newsavedmap.start_masterlocation_id}).i_name
end
if [email protected]?
@waypoint_i_name = Masterlocation.find(:first, :conditions => {:id => @waypoint.waypoint_masterlocation_id}).i_name
end
if [email protected]_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
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.
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.
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.
Code from 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 [email protected]_masterlocation_id.nil?
@start_inst_name = Masterlocation.find(:first, :conditions => {:id => @newsavedmap.start_masterlocation_id}).inst_name
end
if [email protected]_masterlocation_id.nil?
@end_inst_name = Masterlocation.find(:first, :conditions => {:id => @newsavedmap.end_masterlocation_id}).inst_name
end
else
@newsavedmap = Newsavedmap.new
end
You're asking ActiveRecord to find all the waypoints that match your conditions:
@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 = 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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments