Rails 5:接受嵌套属性

时间:2017-09-19 15:06:51

标签: ruby-on-rails ruby devise ruby-on-rails-5

我试图在我的应用上实施accepts_nested_attributes_for。当我向accepts_nested_attributes_for :account添加user.rb时,嵌套字段会消失。删除accepts_nested_attributes_for :account后,当我点击submit时,会显示字段但没有保存数据。

为什么会发生这种情况?

我添加了两个具有适当关联的模型:

user.rb

has_one :account
has_many :items
accepts_nested_attributes_for :account

account.rb

belongs_to :user

应用程序/控制器/用户/ registrations.controller.rb

before_action :configure_permitted_parameters, if: :devise_controller?

def new

  build_resource({})
  resource.build_account
  respond_with self.resource
  session[:registration_params] = request.query_parameters
end

protected

   def configure_permitted_parameters
     devise_parameter_sanitizer.permit(:sign_up) { |u|
       u.permit(:email, :password, :password_confirmation, :remember_me,
                :account_attributes => [:first_name, :last_name, :buisness_name,
                                    :buisness_description, :web_site, :phone_number,
                                    :street, :city, :state, :zip_code, :country])
 }
   end

new.html.erb

<h2>Sign up</h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :password %>
    <% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %>
 </div>

<%= f.fields_for :account do |form| %>
    <div class="field">
      <%= form.label :first_name %>
      <%= form.text_field :first_name, id: :account_first_name %>
    </div>

    <div class="field">
      <%= form.label :last_name %>
      <%= form.text_field :last_name, id: :account_last_name %>
    </div>

    <div class="field">
      <%= form.label :buisness_name %>
      <%= form.text_field :buisness_name, id: :account_buisness_name %>
    </div>

    <div class="field">
      <%= form.label :buisness_description %>
      <%= form.text_field :buisness_description, id: :account_buisness_description %>
    </div>

    <div class="field">
      <%= form.label :web_site %>
      <%= form.text_field :web_site, id: :account_web_site %>
    </div>

    <div class="field">
      <%= form.label :phone_number %>
      <%= form.text_field :phone_number, id: :account_phone_number %>
    </div>

    <div class="field">
      <%= form.label :street %>
      <%= form.text_field :street, id: :account_street %>
    </div>

    <div class="field">
      <%= form.label :city %>
      <%= form.text_field :city, id: :account_city %>
    </div>

    <div class="field">
      <%= form.label :state %>
      <%= form.text_field :state, id: :account_state %>
    </div>

    <div class="field">
      <%= form.label :zip_code %>
      <%= form.text_field :zip_code, id: :account_zip_code %>
    </div>

    <div class="field">
      <%= form.label :country %>
      <%= form.text_field :country, id: :account_country %>
    </div>
<% end %>

 <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

2 个答案:

答案 0 :(得分:1)

我必须将控制器添加到routes.rb devise_for :users

devise_for :users, controllers: {registrations: 'users/registrations'}

答案 1 :(得分:0)

您可以将 cocoon gem用于嵌套属性。它将使处理嵌套属性更加容易