简体   繁体   中英

RoR wrong number of arguments (0 for 1) form

Hi I'm trying to create a form, that at the same time, creates a list and associates products to it.

The problem is that the form keeps raising

wrong number of arguments (0 for 1)

Extracted source (around line #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 is

    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'

The code is as follows:

---view----

--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 %>

---model---

--list--

      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

--product--

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

---controllers--- --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

You were right, I actually realized it after posting this, but now while trying to submit the form this happens:

The form contains 1 error. * Name can't be blank

event tough I filled it correctly, this is what is getting passed

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess utf8: ✓ authenticity_token: 38CXjVORlj2RBgoTetIMoHomcVgOIlBU5rW3NTgkRkU= list: !ruby/hash:ActiveSupport::HashWithIndifferentAccess name: list description: this is a list products_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess '0': !ruby/hash:ActiveSupport::HashWithIndifferentAccess name: p1 description: this is a product commit: Create action: create controller: lists

Where did that l come from? I'm pretty sure you need to change it to 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 %>

Update

There are a few problems with your code. First of all when you call @list = current_user.lists.build(params[:list]) if signed_in? it means that if there is no user signed in that object won't be created at all. The proper way to do something like this would be with a before_filter in your controller.

Secondly @product = @product.build(params[:product]) won't work. You haven't initialized a Product object yet, and you haven't assigned it to @product yet. Also build is used for associations. You need to change this to @product = Product.new(params[:product]) .

Lists controller:

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

Products controller:

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

As far as I remember however, the products#create action won't be used when saving a product through a nested form, the lists#create action will be used for both.

To learn more about nested forms have a look at these railscasts .

Once you've updated your code and gone through those videos, if you're still getting errors I would recommend to create a new question since this one is getting long and messy already :)

您忘记这样做:

rails generate migration add_remember_token_to_users

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM