[英]ArgumentError (wrong number of arguments (2 for 1)) - `find' and `create' for RoR app
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. 我的Ruby on Rails应用程序有一个HTML表单和两个MYSQL数据库表。 My first table has "masterlocations": points of interests with nicknames, IDs, and addresses.
我的第一个表具有“ masterlocations”:带有昵称,ID和地址的兴趣点。 The form lets the user select a "masterlocation" by its nickname from a drop-down.
该表格允许用户从下拉列表中通过昵称选择“ masterlocation”。 My second table, newsavedmap, saves the masterlocation that the user selects.
我的第二张表newsavedmap保存用户选择的主位置。
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. 查看在下拉列表中选择的masterlocation昵称是否与我的masterlocation数据库中的任何masterlocation的昵称匹配。
If so, I want to save the masterlocations's ID to @newsavedmap.start_masterlocation_id. 如果是这样,我想将masterlocation的ID保存到@ 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. 我想我快要到了,但是我的当前代码出现了“ ArgumentError(错误的参数数量(2为1))”。
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]}) 发生在@ 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 更新1
Thanks to vinodadhikary, I've added the Masterlocation line below, but I updated it with => and a colon. 感谢vinodadhikary,我在下面添加了Masterlocation行,但是我用=>和一个冒号更新了它。 However, the ID is not being saved to the database.
但是,该ID未保存到数据库中。 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
. Masterlocation.all
返回一个Array
,当您执行Array.find()
时,该Array
又返回Enumerator
。
You can check these by testing the following in your rails console
: 您可以通过在
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. 因此,
#find
方法方法仅采用一个参数(参考文档: http : #find
),并且您试图提供两个参数,因此会出现错误。
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
,然后在ruby端而不是通过ActiveRecords在数据库中使用find
,如下所示?
Masterlocation.find(:all, conditions: { :id => params[:id], :nickname => params[:startthere]})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.