【问题标题】:Could not save values in Nested forms using rails 5无法使用 rails 5 在嵌套表单中保存值
【发布时间】:2016-11-03 10:55:36
【问题描述】:

我一直坚持使用 Ruby on Rails 构建嵌套表单。 我正在尝试构建一个包含三个表(User, Contact, Address)的字段的表单。用户表有address_idcontact_id。用户填写详细信息时,联系人详细信息应保存在contact 表中,地址应保存在address 表中。这两个 ID 都应与用户详细信息一起保存在用户表中。我应该如何进行?

我的模特,

class Address < ApplicationRecord
  has_one :user
end

class Contact < ApplicationRecord
  has_one :user
end

class User < ApplicationRecord
  belongs_to :address
  belongs_to :contact 
end

我的控制器,

class UsersController < ApplicationController
  def new
    @user = User.new
    @user.build_contact
    @user.build_address
  end
  def create
    @user = User.new(user_params)
    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end
  private
  def user_params
    params.require(:user).permit(:name, :email, contact_attributes: [:phone], address_attributes: [:street, :city])
  end
end

我的观点是,

<%= form_for(user) do |f| %>
  <% if user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
      <ul>
      <% user.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %>
    <%= f.text_field :name %>
 </div>

 <div class="field">
   <%= f.label :email %>
   <%= f.text_field :email %>
 </div>

 <%= f.fields_for :contact do |c| %>
   <div class="field">
     <%= c.label :phone %>
     <%= c.text_field :phone %>
   </div>
 <% end %>

 <%= f.fields_for :address do |a| %>
   <div class="field">
     <%= a.label :street %>
     <%= a.text_field :street %>
   </div>

   <div class="field">
     <%= a.label :city %>
     <%= a.text_field :city %>
   </div>
 <% end %>

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

我的方法对吗?请建议。提前致谢。

【问题讨论】:

    标签: ruby-on-rails nested-forms relationships model-associations


    【解决方案1】:

    你漏掉了几行...

    class User < ApplicationRecord
      belongs_to :address
      belongs_to :contact
      accepts_nested_attributes_for :address
      accepts_nested_attributes_for :contact 
    end
    

    同时确保您接受:id:_delete

    params.require(:user).permit(:name, :email, contact_attributes: [:id, :phone, :_delete], address_attributes: [:id, :street, :city, :_delete]
    

    【讨论】:

    • 非常感谢史蒂夫先生。有效。另一个疑问,我什么时候应该使用 user.contact.build ?
    • my_objectsomething之间的一对一关联时使用my_object.build_something,但当my_object和@987654330之间的一对多关联时使用my_object.somethings.build @
    • 感谢史蒂夫先生的澄清。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    相关资源
    最近更新 更多