1

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:

  1. See if the masterlocation nickname selected in the dropdown matches the nickname of any of the masterlocations in my masterlocation database.

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

1 回答 1

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]}

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

Masterlocation.find(:all, conditions: { :id => params[:id], :nickname => params[:startthere]})
于 2013-08-04T00:33:55.357 回答