Rails嵌套表单未插入数据库

时间:2015-09-07 04:04:41

标签: ruby-on-rails nested-forms

好的,所以我有两个表的rails嵌套表单。它将所有内容都插入到一个表中,但不在另一个表中。我已经查看了所有可以得到的示例,无论我做什么,它似乎都不起作用。

以下是我的模特:

用户:

class User < ActiveRecord::Base
    has_many :addresses, dependent: :destroy
    accepts_nested_attributes_for :addresses
end

地址:

class Address < ActiveRecord::Base
    belongs_to :user
end

这是我的表格:

= form_for @user do |f|
  - if @user.errors.any?
    #error_explanation
      %h2= "#{pluralize(@user.errors.count, "error")} prohibited this user from being saved:"
      %ul
        - @user.errors.full_messages.each do |msg|
          %li= msg
  %ul
  .field
    = f.label :first_name
    = f.text_field :first_name
  .field
    = f.label :last_name
    = f.text_field :last_name
  .field
    = f.label :password
    = f.text_field :password
  .field
    = f.label :email
    = f.text_field :email
  = f.fields_for :address do |address|
    .field 
      = address.label :country 
      = address.text_field :country
    .field 
      = address.label :state
      = address.text_field :state
    .field
      = address.label :city 
      = address.text_field :city
    .field 
      = address.label :zip_code
      = address.text_field :zip_code 
  .actions
    = f.submit 'Save'

最后是控制器,我相信这是我问题的根源,我已经查阅了无数关于如何做到这一点的例子,看起来每个人都有不同的看法。无论如何它是:

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]

  # GET /users
  # GET /users.json
  def index
    @users = User.all
  end

  # GET /users/1
  # GET /users/1.json
  def show
  end

  # GET /users/new
  def new
    @user = User.new
    @addresses = @user.addresses.build
  end

  # GET /users/1/edit
  def edit
  end

  # POST /users
  # POST /users.json
  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

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:first_name, :last_name, :password, :email, 
      address_attributes: [:country, :state, :city])
    end
end

2 个答案:

答案 0 :(得分:0)

是否可以在表单中重写以下代码行,因为has_many关系,address将以addresses形式更改为plural

  = fields_for :address do |address|

替换为

   = f.fields_for :addresses do |address|

user_params应该稍微更改一下,例如addresses_attributes而不是address_attributes

def user_params
  params.require(:user).permit(:first_name, :last_name, :password, :email, 
  addresses_attributes: [:country, :state, :city])
end

答案 1 :(得分:0)

= fields_for :address do |address|

将是

= f.fields_for :addresses do |address|

在您的控制器中,它将是addresses_attributes

def user_params
      params.require(:user).permit(:first_name, :last_name, :password, :email, 
      addresses_attributes: [:country, :state, :city])
    end

您可以将cocoon gem用于嵌套属性。这很好,很容易实现。