感谢您提供的任何帮助!我已经回顾了与此错误相关的其他主题,但未能找到解决方案。
我的Ruby on Rails应用程序有一个HTML表单和两个MYSQL数据库表。我的第一个表有“masterlocations”:带有昵称,ID和地址的兴趣点。该表单允许用户通过下拉列表中的昵称选择“masterlocation”。我的第二个表newsavedmap保存了用户选择的masterlocation。
我的目标是:
查看下拉列表中选择的masterlocation昵称是否与我的主列表数据库中任何masterlocations的昵称相匹配。
如果是这样,我想将masterlocations的ID保存到@ newsavedmap.start_masterlocation_id。
我想我差不多了,但是我用现有的代码得到了“ArgumentError(错误的参数数量(2 for 1))”。
控制器
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
错误代码
出现在@ 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'
更新1
感谢vinodadhikary,我在下面添加了Masterlocation行,但我用=>更新了它。和一个冒号。但是,ID未保存到数据库中。相反,记录的字段用
更新 --- []
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
答案 0 :(得分:0)
这里要注意几点。 Masterlocation.all
会返回Array
,当您执行Array.find()
时,它会返回Enumerator
。
您可以通过在rails console
中测试以下内容来检查这些内容:
@masterlocation.class
=> Array
@masterlocation.find().class
=> Enumerator
因此#find
方法方法只接受一个参数(参考文档:http://rdoc.info/stdlib/core/1.9.3/Enumerable#find-instance_method)并且您尝试提供两个,因此错误。
解决这个问题的方法是:
@masterlocation.find{|ml| ml.id == params[:id] and ml.nickname == params[:startthere]}
但问题是你为什么要检索所有Masterlocation
然后在ruby端而不是在数据库中使用find
通过ActiveRecords,如下所示?
Masterlocation.find(:all, conditions: { :id => params[:id], :nickname => params[:startthere]})