Thanks for any help you can provide! I've reviewed other topics related to this error but haven't been able to find a solution for my situation.
I've got an HTML form and two MYSQL database tables for my Ruby on Rails app. My first table has "masterlocations": points of interests with nicknames, IDs, and addresses. The form lets the user select a "masterlocation" by its nickname from a drop-down. My second table, newsavedmap, saves the masterlocation that the user selects.
My goal is to:
See if the masterlocation nickname selected in the dropdown matches the nickname of any of the masterlocations in my masterlocation database.
If so, I want to save the masterlocations's ID to @newsavedmap.start_masterlocation_id.
I think I'm almost there, but I'm getting the "ArgumentError (wrong number of arguments (2 for 1))" with my current code.
Controller
def create
@newsavedmap = Newsavedmap.new(params[:newsavedmap])
@masterlocation = Masterlocation.all
@newsavedmap.name = params[:newsavedmapname]
if !params[:starthere].blank?
@newsavedmap.start = params[:starthere]
else
@newsavedmap.start = params[:startthere]
@newsavedmap.start_masterlocation_id = @masterlocation.find(params[:id], :conditions => {:nickname => params[:startthere]})
end
if !params[:endhere].blank?
@newsavedmap.end = params[:endhere]
else
@newsavedmap.end = params[:endthere]
end
if !params[:waypointsselected].blank?
@waypoint = Waypoint.new(params[:waypoint])
@waypoint.waypoint = params[:waypointsselected]
@waypoint.newsavedmap = @newsavedmap
end
respond_to do |format|
if @newsavedmap.save
# flash[:notice] = 'The new map was successfully created.'
# format.html { redirect_to "/itineraries/#{@newsavedmap.itinerary_id}#showmaps" }
format.html { redirect_to "/maptry" }
format.xml { render :xml => @newsavedmap, :status => :created, :location => @newsavedmap }
else
format.html { render :action => "new" }
format.xml { render :xml => @newsavedmap.errors, :status => :unprocessable_entity }
end
if @waypoint.save
# flash[:notice] = 'The new map was successfully created.'
# format.html { redirect_to "/itineraries/#{@newsavedmap.itinerary_id}#showmaps" }
format.html { redirect_to "/maptry" }
format.xml { render :xml => @waypoint, :status => :created, :location => @waypoint }
else
format.html { render :action => "new" }
format.xml { render :xml => @waypoint.errors, :status => :unprocessable_entity }
end
end
end
ERROR CODE
Occurs in the line for @newsavedmap.start_masterlocation_id = @masterlocation.find(params[:id], :conditions => {:nickname => params[:startthere]})
ArgumentError (wrong number of arguments (2 for 1)):
app/controllers/newsavedmaps_controller.rb:66:in `find'
app/controllers/newsavedmaps_controller.rb:66:in `create'
UPDATE 1
Thanks to vinodadhikary, I've added the Masterlocation line below, but I updated it with => and a colon. However, the ID is not being saved to the database. Instead, the record's field updates with
--- []
CODE
def create
@newsavedmap = Newsavedmap.new(params[:newsavedmap])
@newsavedmap.name = params[:newsavedmapname]
if !params[:starthere].blank?
@newsavedmap.start = params[:starthere]
else
@newsavedmap.start = params[:startthere]
@newsavedmap.start_masterlocation_id = Masterlocation.find(:all, :conditions => { :id => params[:id], :nickname => params[:startthere]})
end
A couple of points to note here. The Masterlocation.all
returns an Array
and when you do Array.find()
then that in turn returns Enumerator
.
You can check these by testing the following in your rails console
:
@masterlocation.class
=> Array
@masterlocation.find().class
=> Enumerator
So the #find
method method only takes in one parameter(Ref doc: http://rdoc.info/stdlib/core/1.9.3/Enumerable#find-instance_method ) and you are trying to supply two, hence the error.
The fix for this would be to do:
@masterlocation.find{|ml| ml.id == params[:id] and ml.nickname == params[:startthere]}
But then the question is why do you want to retrieve all the Masterlocation
s and then use find
on the ruby side instead of in the database via ActiveRecords as follows?
Masterlocation.find(:all, conditions: { :id => params[:id], :nickname => params[:startthere]})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.