简体   繁体   English

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

[英]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: 我的目标是:

  1. See if the masterlocation nickname selected in the dropdown matches the nickname of any of the masterlocations in my masterlocation database. 查看在下拉列表中选择的masterlocation昵称是否与我的masterlocation数据库中的任何masterlocation的昵称匹配。

  2. 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.

相关问题 “参数数量错误(2为1)(ArgumentError)”的问题 - Problems with “wrong number of arguments (2 for 1) (ArgumentError)” Sinatra:参数数量错误(0..2为4) - Sinatra: wrong number of arguments (4 for 0..2) Ruby MySQL错误的参数数量 - ruby mysql wrong number of arguments ProgrammingError:字符串格式化过程中的参数数量错误 - ProgrammingError: Wrong number of arguments during string formatting 使用ROR应用程序设置mysql数据库时出现ArgumentError - ArgumentError while setting up mysql database with ROR application 使用动态数量的参数创建MySQL存储函数 - Create a MySQL stored function with a dynamic number of arguments Rake不会回滚迁移,参数数量错误(1代表0) - Rake won't rollback migrations, wrong number of arguments (1 for 0) 编程错误:字符串格式化期间的参数数量错误,尝试过元组修复 - ProgrammingError: Wrong number of arguments during string formatting, tried tuple fix Angular,Flask,mysql.connector编程错误:参数数量错误 - Angular, Flask, mysql.connector ProgrammingError: Wrong number of arguments 在ruby中连接到mysql会产生错误数量的参数错误(4之0)-如何调试? - Connecting to mysql in ruby produces wrong number of arguments error (4 of 0) - how to debug?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM