参数错误/错误的参数数量 - 我不知道这里发生了什么,

时间:2014-06-07 08:34:47

标签: ruby-on-rails ruby-on-rails-4 controller arguments params

我正在尝试使用paperclip并更新列表,但每当我在表单上执行此操作时,都会收到此错误。

ListingsController#update中的ArgumentError 错误的参数数量(2对1)

代码如下,如果@ listing.update(listing_params)

,那么突出显示为错误的行将以开头
     @listing = Listing.new
    respond_to do |format|
      if @listing.update(listing_params)
        format.html { redirect_to @listing, notice: 'Listing was successfully updated.' }
        format.json { head :no_content }
      else

这是应用程序跟踪

app/controllers/listings_controller.rb:45:in `block in update'
app/controllers/listings_controller.rb:44:in `update'

有什么我缺少的,语法方面的,还是其他的东西?非常感谢。

编辑:

我的列表模型(listings.rb)的代码如下

class Listing < ActiveRecord::Base
  has_attached_file :image, :styles => { :medium => "200x", :thumb => "100x100>" }, :default_url  => "default.jpg"
end

我的表单(_form.html.erb)如下:

<%= form_for @listing, :html => { :multipart => true } do |f| %>
  <% if @listing.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@listing.errors.count, "error") %> prohibited this listing from being saved:</h2>

      <ul>
      <% @listing.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name, class: "form-control" %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description, class: "form-control" %>
  </div>
  <div class="field">
    <%= f.label :price %><br>
    <%= f.text_field :price, class: "form-control" %>
  </div>
  <div class="form-group">
    <%= f.file_field :image, class: "form-control" %>
  </div>
  <div class="actions">
    <%= f.submit class: "btn btn-primary" %>
  </div>
<% end %>

2 个答案:

答案 0 :(得分:1)

您正在更新商家信息。你需要删除

  

@listing = Listing.new

通过写这个,你正在初始化一个列表。这是一种更新方法,因此您需要找到您的商家信息而不是初始化新商品,请尝试

@listing = Listing.find(params[:id]) #this will find your listing with id
respond_to do |format|
  if @listing.update(listing_params) #this will update the listing which you find above
    format.html { redirect_to @listing, notice: 'Listing was successfully updated.' }
    format.json { head :no_content }
  else
    #code
  end
end

答案 1 :(得分:0)

您更新控制器方法应如下所示;

def update
    @listing = Listing.find(params[:id]) #find item you want to update

    respond_to do |format|
      if @listing.update(listing_params) # the refer to it and update it
        format.html { redirect_to @listing, notice: 'Listing was successfully updated.' }
        format.json { head :no_content }
      else
    end
  end