簡體   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