RoR错误的参数数量(0表示1)形式

时间:2013-04-27 15:02:06

标签: ruby-on-rails nested-attributes

您好我正在尝试创建一个表单,同时创建一个列表并将产品与其关联。

问题在于表格不断提升

错误的参数数量(0表示1)

提取的来源(第10行):

     7:     <%= f.text_area :description, placeholder:
     8:     "Compose a description for it ..." %>
     9: </div>
     10: <%= l.fields_for :products do |builder| %>
     11: <%= render 'shared/product_form', :l => builder %>
     12: <% end %>
     13: <%= l.submit "Create", class: "btn btn-large btn-primary" %>

App Trace是

    app/views/shared/_list_form.html.erb:10:in `block in        _app_views_shared__list_form_html_erb__184644094_33330696'
    app/views/shared/_list_form.html.erb:1:in `_app_views_shared__list_form_html_erb__184644094_33330696'
    app/views/lists/new.html.erb:7:in `_app_views_lists_new_html_erb__973495114_33282228'

代码如下:

--- ----视图

- list_form -

    <%= form_for(@list) do |f| %>
        <%= render 'shared/error_messages', object: f.object %>
        <div class="field">
    <%= f.text_field :name, placeholder:
              "Come up with a name for your list" %>

    <%= f.text_area :description, placeholder:
              "Compose a description for it ..." %>
        </div>
        <%= f.fields_for :products do |builder| %>
        <%= render 'shared/product_form', :f => builder %>
        <% end %>
        <%= f.submit "Create", class: "btn btn-large btn-primary" %>
        <% end %>

- product_form -

     <%= f.text_field :name, "Name:" %>
     <%= f.text_area :description, :rows => 3 %>

--- ---模型

- 列表 -

      class List < ActiveRecord::Base
         attr_accessible :description, :name
         belongs_to :user
         has_many :products, :dependent => :destroy
         accepts_nested_attributes_for :products,  :reject_if => lambda { |a|                                 a[:name].blank? }, :allow_destroy => true

         has_many :list_categorization
         has_many :category, :through => :list_categorization

         validates :user_id, presence: true
         validates :name, presence: true, length: {maximum: 10}
         validates :description, length: {maximum: 140}

         default_scope order: 'lists.created_at DESC'

         def categorize!(category_id)
             list_categorization.create!(category_id: category_id)
         end
      end

- 产品 -

    class Product < ActiveRecord::Base
         attr_accessible :description, :donated, :name
         validates :list_id, presence: true
         belongs_to :list
    end

--- ---控制器 --list_controller -

   def new
       @list = List.new
       @products = @list.products.build
   end

   def create
       @list = current_user.lists.build(params[:list]) if signed_in?
       if @list.save
       flash[:success] ="List " + @list.name + "created!" 
       render 'new'
   end

- product_controller -

  def new
      @product = Product.new
  end

  def create
      @product = @product.build(params[:product]) if signed_in?
      if @product.save
      flash[:success] ="Product " + @product.name + "created!" 
  end

你是对的,我在发布此内容后实际意识到了这一点,但现在在尝试提交表单时会发生这种情况:

表单包含1个错误。 *名称不能为空

事件艰难我填写正确,这就是通过

---!ruby / hash:ActiveSupport :: HashWithIndifferentAccess utf8:✓ authenticity_token:38CXjVORlj2RBgoTetIMoHomcVgOIlBU5rW3NTgkRkU = list:!ruby / hash:ActiveSupport :: HashWithIndifferentAccess   名单   说明:这是一个清单   products_attributes:!ruby / hash:ActiveSupport :: HashWithIndifferentAccess     '0':! ruby​​ / hash:ActiveSupport :: HashWithIndifferentAccess       名称:p1       描述:这是一个产品 提交:创建 行动:创造 控制器:列表

2 个答案:

答案 0 :(得分:1)

l来自哪里?我很确定你需要将其更改为f

<%= form_for(@list) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_field :name, placeholder: "Come up with a name for your list" %>
    <%= f.text_area :description, placeholder: "Compose a description for it ..." %>
  </div>
  <%= f.fields_for :products do |builder| %>
    <%= render 'shared/product_form', :l => builder %>
  <% end %>
  <%= f.submit "Create", class: "btn btn-large btn-primary" %>
<% end %>

更新

您的代码存在一些问题。首先,当你致电@list = current_user.lists.build(params[:list]) if signed_in?时,这意味着如果没有用户登录,则根本不会创建该对象。执行此类操作的正确方法是使用控制器中的before_filter

其次@product = @product.build(params[:product])不起作用。您尚未初始化Product对象,但尚未将其分配给@productbuild也用于关联。您需要将其更改为@product = Product.new(params[:product])

列出控制器:

before_filter :user_signed_in? # add to products controller as well
# if you need this filter only on certain actions then do:
# before_filter :user_signed_in?, only: [:new, :create]

def new
  @list = current_user.lists.build
  @products = @list.products.build
end

def create
  @list = current_user.lists.build(params[:list])
  if @list.save
    flash[:success] = "List " + @list.name + " created!"
    redirect_to lists_path # this part was missing!
  else # this was also missing
    render 'new'
  end # you had an 'if' with no 'end'
end

private

# add the following to Products controller as well, or if you
# use it a lot then place it in your application controller
def user_signed_in?
  unless signed_in?
    flash[:notice] = "You must first sign in"
    redirect_to sign_in_path
  end
end

产品控制器:

def new
  @product = Product.new
end

def create
  @product = Product.new(params[:product]
  if @product.save
    flash[:success] = "Product " + @product.name + " created!"
    redirect_to @product
  else
    render 'new'
  end
end

据我记得,在通过嵌套表单保存产品时不会使用products#create操作,列表#create操作将用于两者。

要了解有关嵌套表单的更多信息,请查看these railscasts

一旦您更新了代码并浏览了这些视频,如果您仍然遇到错误,我建议您创建一个新问题,因为这个问题已经变得冗长而混乱:)

答案 1 :(得分:0)

你忘记这样做了:

rails generate migration add_remember_token_to_users