Rails 4 嵌套模型:不工作
Rails 4 Nested Model : Not working
我曾尝试使用简单的嵌套模型和表单,但最终失败了。我不明白我做错了什么?尝试实现简单的嵌套模型。一个 parent 有很多 child。谁能帮帮我。谢谢。
以下是模型:
parent.rb
class Parent < ActiveRecord::Base
has_many :childs
accepts_nested_attributes_for :childs
end
child.rb
class Child < ActiveRecord::Base
belongs_to :parent
end
parents_controller.rb
class ParentsController < ApplicationController
before_action :set_parent, only: [:show, :edit, :update, :destroy]
# GET /parents
# GET /parents.json
def index
@parents = Parent.all
end
# GET /parents/1
# GET /parents/1.json
def show
end
# GET /parents/new
def new
@parent = Parent.new
@parent.childs.new
end
# GET /parents/1/edit
def edit
end
# POST /parents
# POST /parents.json
def create
@parent = Parent.create(parent_params)
respond_to do |format|
if @parent.save
format.html { redirect_to @parent, notice: 'Parent was successfully created.' }
format.json { render :show, status: :created, location: @parent }
else
format.html { render :new }
format.json { render json: @parent.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /parents/1
# PATCH/PUT /parents/1.json
def update
respond_to do |format|
if @parent.update(parent_params)
format.html { redirect_to @parent, notice: 'Parent was successfully updated.' }
format.json { render :show, status: :ok, location: @parent }
else
format.html { render :edit }
format.json { render json: @parent.errors, status: :unprocessable_entity }
end
end
end
# DELETE /parents/1
# DELETE /parents/1.json
def destroy
@parent.destroy
respond_to do |format|
format.html { redirect_to parents_url, notice: 'Parent was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_parent
@parent = Parent.find(params[:id])
end
def parent_params
params.require(:parent).permit(:name, childs_attributes: [:name])
end
end
parent form.html.erb
<%= form_for(@parent) do |f| %>
<% if @parent.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@parent.errors.count, "error") %> prohibited this parent from being saved:</h2>
<ul>
<% @parent.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label 'Parent name' %><br>
<%= f.text_field :name %>
</div>
<%= f.fields_for :child do |c| %>
<div class="field">
<%= c.label 'Child Name' %><br>
<%= c.text_field :name %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
正在创建 parent,但未创建 child。 child table 具有 parent_id 用于关联两个模型。
提前致谢
我相信child的复数不是childs
,而是children
,Rails知道这一点。您需要相应地更改 has_many
关联。
编辑:正如@pavan 指出的那样,更改代码中所有出现的地方,而不仅仅是关联。
我曾尝试使用简单的嵌套模型和表单,但最终失败了。我不明白我做错了什么?尝试实现简单的嵌套模型。一个 parent 有很多 child。谁能帮帮我。谢谢。
以下是模型:
parent.rb
class Parent < ActiveRecord::Base
has_many :childs
accepts_nested_attributes_for :childs
end
child.rb
class Child < ActiveRecord::Base
belongs_to :parent
end
parents_controller.rb
class ParentsController < ApplicationController
before_action :set_parent, only: [:show, :edit, :update, :destroy]
# GET /parents
# GET /parents.json
def index
@parents = Parent.all
end
# GET /parents/1
# GET /parents/1.json
def show
end
# GET /parents/new
def new
@parent = Parent.new
@parent.childs.new
end
# GET /parents/1/edit
def edit
end
# POST /parents
# POST /parents.json
def create
@parent = Parent.create(parent_params)
respond_to do |format|
if @parent.save
format.html { redirect_to @parent, notice: 'Parent was successfully created.' }
format.json { render :show, status: :created, location: @parent }
else
format.html { render :new }
format.json { render json: @parent.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /parents/1
# PATCH/PUT /parents/1.json
def update
respond_to do |format|
if @parent.update(parent_params)
format.html { redirect_to @parent, notice: 'Parent was successfully updated.' }
format.json { render :show, status: :ok, location: @parent }
else
format.html { render :edit }
format.json { render json: @parent.errors, status: :unprocessable_entity }
end
end
end
# DELETE /parents/1
# DELETE /parents/1.json
def destroy
@parent.destroy
respond_to do |format|
format.html { redirect_to parents_url, notice: 'Parent was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_parent
@parent = Parent.find(params[:id])
end
def parent_params
params.require(:parent).permit(:name, childs_attributes: [:name])
end
end
parent form.html.erb
<%= form_for(@parent) do |f| %>
<% if @parent.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@parent.errors.count, "error") %> prohibited this parent from being saved:</h2>
<ul>
<% @parent.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label 'Parent name' %><br>
<%= f.text_field :name %>
</div>
<%= f.fields_for :child do |c| %>
<div class="field">
<%= c.label 'Child Name' %><br>
<%= c.text_field :name %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
正在创建 parent,但未创建 child。 child table 具有 parent_id 用于关联两个模型。
提前致谢
我相信child的复数不是childs
,而是children
,Rails知道这一点。您需要相应地更改 has_many
关联。
编辑:正如@pavan 指出的那样,更改代码中所有出现的地方,而不仅仅是关联。