嵌套形式的Rails [英] Rails with nested form

查看:82
本文介绍了嵌套形式的Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我提交表单时,它会创建一个新的Outfitter,但不会创建一个新的User.在日志中显示不允许的参数:utf8,authenticity_token,first_name,last_name,电子邮件,密码,password_confirmation,提交"

When I submit the form it creates the new Outfitter, but it does not create a new User. In the log it says 'Unpermitted parameters: utf8, authenticity_token, first_name, last_name, email, password, password_confirmation, commit'

模式html :(模式位于application.html.erb中.页面为localhost:3000/pages/index)

Modal html:(modal is in application.html.erb. Page is localhost:3000/pages/index)

<div id="popup-outfitter-signup" class="modal popup">
 <div class="modal-main rounded-10px">
 <div class="modal-title">
  Create your Outfitter<br/>
 </div><!-- end .modal-title --> 
 <%= simple_form_for :outfitter do |f| %>
 <div class="modal-message">
  <p>
  <%= label_tag :name, "Outfitter Name" %><br/>
    <%= text_field_tag :name, params[:name], class: 'input-txt rounded-2px' %>
  </p>
  <p>
    <%= label_tag :address, "Address:" %><br/>
    <%= text_field_tag :address, params[:address], class: 'input-txt rounded-2px' %>
  </p>
  <p>
    <%= label_tag :business_phone, "Phone:" %><br/>
    <%= text_field_tag :business_phone, params[:business_phone], class: 'input-txt rounded-2px' %>
  </p>

    <%= simple_fields_for :users do %>
      <p>
        <%= label_tag :first_name, "First Name" %><br/>
        <%= text_field_tag :first_name, params[:first_name], class: 'input-txt rounded-2px' %>
      </p>
      <p>
        <%= label_tag :last_name, "Last Name:" %><br/>
        <%= text_field_tag :last_name, params[:last_name], class: 'input-txt rounded-2px' %>
      </p>
      <p>
        <%= label_tag :email, "Email:" %><br/>
        <%= text_field_tag :email, params[:email], class: 'input-txt rounded-2px' %>
      </p>
      <p>
        <%= label_tag :password, "Password:" %><br/>
        <%= password_field_tag :password, params[:password], class: 'input-txt rounded-2px' %>
      </p>
      <p>
        <%= label_tag :password_confirmation, "Password Confirmation:" %><br/>
        <%= password_field_tag :password_confirmation, params[:password_confirmation], class: 'input-txt rounded-2px' %>
      </p>
    <% end %>

  <div class="button rounded-2px trans-all">
    <%= submit_tag "Signup", class: 'send-btn rounded-2px trans-all' %>
  </div>
  <br/>
 </div>
 <% end %>
 </div><!-- end .modal-main -->
</div><!-- end .modal -->

outfitters_controller.rb

outfitters_controller.rb

def new
  @outfitter = Outfitter.new
  @outfitter.users.build
end

推荐答案

首先,我建议使用rails simple_form gem(如果您要构建映射到模型的表单.

Firstly I would recommend using rails form_for or the simple_form gem if you are building a form that maps to model.

使用上述任何一项都将允许您使用fields_for:users,这会将users_attributes发送到控制器.

Using any of the above will allow you to use fields_for :users, which will send users_attributes to the controller.

您还需要在Outfitter模型中接受用户:( http: //api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html )

You would also need to accepts_nested_attributes_for :users in your Outfitter model.(http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html)

如果您使用的是Rails 3或Rails 4,则将执行一个额外的步骤:

One extra step that will differ if you are using Rails 3 or Rails 4:

路轨4: 您需要允许在您的强参数上使用users_attributes( http://edgeapi.rubyonrails.org/classes /ActionController/StrongParameters.html )

Rails 4: You need to allow users_attributes on your strong parameters(http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html)

params.require(:outfitter).permit(users_attributes: [])

规则3:您必须将:users_attributes添加到模型的attr_accessible

Rails 3: You would have to add :users_attributes to your model's attr_accessible

class Outfitter < ActiveRecord::Base
    attr_accessible :users_attributes
    accepts_nested_attributes_for :users_attributes
    ...
end

希望这可以为您提供帮助.

Hope this can help you.

使用简单表单时,您需要传递某个类的实例,以便它可以为该特定类构建for并将表单变量传递给块:

When using simple form you need to pass an instance of some class so it can build the for for that specific class and pass a form variable into the block:

# @outfitter will be the one you instantiated your new action
<%= simple_form_for @outfitter do |f| %>
  #form inputs go here, see next code block for more info
<% end %>

简单表单将使用您的@outfitter实例,检查其记录是否保存到数据库中,并定义for的url和方法,并基于该方法在submit上发送数据.

Simple form will take your @outfitter instance, check if its a record saved to the database or not and will define the url and method the for will send the data on submit based on that.

  • 如果@outfitter是新记录(您的情况)

  • if @outfitter is a new record(which is your case)

  • 表单网址将指向"/outfitters"
  • 方法将为POST
  • 这会将请求发送到OutfittersController创建动作
  • form url will point to '/outfitters'
  • method will be POST
  • this will send the request to OutfittersController create action

@outfitter不是新记录(已保存到数据库中,现在正在更新)

if @outfitter is not a new record(has been saved to the database and is now being updated)

  • 表单url将指向'/outfitter/:id(作为第一个简单表单参数传递的@outfitter记录的ID)'
  • 方法将是补丁
  • 这会将请求发送到OutfittersController更新操作
  • form url will point to '/outfitter/:id(the id of the @outfitter record passed as the first simple form argument)'
  • method will be PATCH
  • this will send the request to OutfittersController update action

要在simple_form块内创建输入,请执行以下操作:

To create an input inside the simple_form block you do the following:

<%= simple_form_for @outfitter do |f| %>
  # f - is the form object
  # input - is the method called on the form to create the input
  # :name - is the attribute name that will be mapped to this input
  # you can only create inputs for the attributes in your class
  <%= f.input :name %>
  <%= f.input :address %>
<% end %>

  • 默认情况下,简单表单将使用属性名称为输入创建标签
  • 要使用简单的表单创建嵌套表单,您必须执行以下操作

    To create a nested form with simple form you would have to do the following

    <%= simple_form_for @outfitter do |f| %>
      # calling f.simple_fields_for will ensure simple_form knows the users form is nested under outfitter form
      <%= f.simple_fields_for :users do |user_form| %>
        # user_form is the form object nested under outfitters form
        # every input method inside this block should be called on user_form
        <%= user_form.input :name %>
        <%= user_form.input :address %>
      <% end %>
    <% end %>
    

    您的完整填写如下:

    <%= simple_form_for @outfitter do |f| %>
      <div class="modal-message">
      <p>
        # input_html is used to parse attributes to be added to the input
        <%= f.input :name, input_html: { class: 'input-txt rounded-2px' } %>
      </p>
      <p>
        <%= f.input :address, input_html: { class: 'input-txt rounded-2px' } %>
      </p>
      <p>
        <%= f.input :business_phone, input_html: { class: 'input-txt rounded-2px' } %>
      </p>
    
      <%= f.simple_fields_for :users do |user_form| %>
        <p>
          <%= f.input :first_name, input_html: { class: 'input-txt rounded-2px' } %>
        </p>        
        <p>
          <%= f.input :last_name, input_html: { class: 'input-txt rounded-2px' } %>
        </p>
        <p>
          <%= f.input :email, input_html: { class: 'input-txt rounded-2px' } %>
        </p>
        <p>
          <%= f.input :password, input_html: { class: 'input-txt rounded-2px' } %>
        </p>
        <p>
          <%= f.input :password_confirmation, input_html: { class: 'input-txt rounded-2px' } %>
        </p>
      <% end %>
    
      <div class="button rounded-2px trans-all">
        <%= f.submit 'Signup', class: 'send-btn rounded-2px trans-all' %>
      </div>
    <% end %>
    

    您可以使用简单的表格做更多的事情,请确保转到他们的github存储库并阅读文档.

    There is much more you can do with simple form, make sure you go to their github repo and read the docs.

    享受

    这篇关于嵌套形式的Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆