Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 参数错误/参数数量错误-我不知道是什么';这里发生了什么,_Ruby On Rails_Ruby On Rails 4_Controller_Arguments_Params - Fatal编程技术网

Ruby on rails 参数错误/参数数量错误-我不知道是什么';这里发生了什么,

Ruby on rails 参数错误/参数数量错误-我不知道是什么';这里发生了什么,,ruby-on-rails,ruby-on-rails-4,controller,arguments,params,Ruby On Rails,Ruby On Rails 4,Controller,Arguments,Params,我试图使用回形针更新列表,但每当我在表单上这样做时,我就会收到这个错误 ListingsController更新中的ArgumentError 参数数量错误(2对1) 代码如下,突出显示为有故障的行以if@listing.update(listing_params) 这是应用程序跟踪 app/controllers/listings_controller.rb:45:in `block in update' app/controllers/listings_controller.rb:44:in

我试图使用回形针更新列表,但每当我在表单上这样做时,我就会收到这个错误

ListingsController更新中的ArgumentError 参数数量错误(2对1)

代码如下,突出显示为有故障的行以if@listing.update(listing_params)

这是应用程序跟踪

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
类列表{:medium=>“200x”,:thumb=>“100x100>”},:default\u url=>“default.jpg”
结束
我的表单(_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 %>
{:multipart=>true}do | f |%>
禁止保存此列表:




您正在更新您的列表。您需要删除

@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

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

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

你能发布你的列表参数方法吗?def listing_params.require(:listing).permit(:name,:description,:price,:image)end'code'@user3716559,通过添加模型描述和用于更新的表单来编辑您的问题。我确实更改了这一点,但它为以下行显示了“参数数目错误”错误:“if@listing.update(listing_params)#立即引用并更新它“@user3716559”您的更新方法是什么样子?
def update@listing=listing.find(params[:id])#查找您要更新的项目响应| format | if@listing.update(listing#params)#引用并更新它format.html{将_重定向到@listing,注意:'列表已成功更新。}format.json{head:no_content}else format.html{render action:'edit'}format.json{render json:@listing.errors,status::unprocessable_entity}结束
很抱歉,这里看起来不太吸引人,它是一样的,除了您放置#code的地方,我有一些允许编辑或列出错误的操作。@user3716559这种类型的错误通常在传递的参数数量不匹配时出现。您能试着使用update_属性(列出参数)吗。如果@listing'仍被突出显示为产生了“错误数量的参数(2对1)”@user3716559,请为您的表单和列表模型添加代码,以便更好地理解您在更新过程中传递的信息。@Acacia他使用的是rails4,rails4使用的是强参数
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