Rails 嵌套表单未插入数据库
Rails nested form is not inserting into database
好的,所以我有两个 table 的 rails 嵌套表单。它将所有内容插入一个 table 但不插入另一个。我已经查阅了所有我可以动手的例子,但无论我做什么,它似乎都不起作用。
这是我的模型:
用户:
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
能否请你在你的表单中重写下面这行代码,因为has_many
关系,address
将在plural
表单中更改为addresses
。
= 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
= 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。它很好并且很容易实现。
好的,所以我有两个 table 的 rails 嵌套表单。它将所有内容插入一个 table 但不插入另一个。我已经查阅了所有我可以动手的例子,但无论我做什么,它似乎都不起作用。
这是我的模型:
用户:
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
能否请你在你的表单中重写下面这行代码,因为has_many
关系,address
将在plural
表单中更改为addresses
。
= 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
= 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。它很好并且很容易实现。