繁体   English   中英

ArgumentError(错误的参数数量(2为1))-RoR应用程序的“查找”和“创建”

[英]ArgumentError (wrong number of arguments (2 for 1)) - `find' and `create' for RoR app

感谢您的任何帮助,您可以提供! 我已经审查了与此错误相关的其他主题,但无法为我的情况找到解决方案。

我的Ruby on Rails应用程序有一个HTML表单和两个MYSQL数据库表。 我的第一个表具有“ masterlocations”:带有昵称,ID和地址的兴趣点。 该表格允许用户从下拉列表中通过昵称选择“ masterlocation”。 我的第二张表newsavedmap保存用户选择的主位置。

我的目标是:

  1. 查看在下拉列表中选择的masterlocation昵称是否与我的masterlocation数据库中的任何masterlocation的昵称匹配。

  2. 如果是这样,我想将masterlocation的ID保存到@ newsavedmap.start_masterlocation_id。

我想我快要到了,但是我的当前代码出现了“ ArgumentError(错误的参数数量(2为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未保存到数据库中。 而是,记录的字段更新为

    --- []

    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

这里有几点要注意。 Masterlocation.all返回一个Array ,当您执行Array.find()时,该Array又返回Enumerator

您可以通过在rails console测试以下内容来检查这些内容:

@masterlocation.class
=> Array

@masterlocation.find().class
=> Enumerator

因此, #find方法方法仅采用一个参数(参考文档: http : #find ),并且您试图提供两个参数,因此会出现错误。

解决此问题的方法是:

@masterlocation.find{|ml| ml.id == params[:id] and ml.nickname == params[:startthere]}

但是问题是,为什么要检索所有Masterlocation ,然后在ruby端而不是通过ActiveRecords在数据库中使用find ,如下所示?

Masterlocation.find(:all, conditions: { :id => params[:id], :nickname => params[:startthere]})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM