我正在尝试使用fields_for
并创建一个嵌套表单,但只显示一个文本字段,为空。我有3 crewmember
条记录。
crewmember
型号:
class Crewmember < ActiveRecord::Base
belongs_to :production
belongs_to :callsheet
validates :firstname, presence: true
validates :email, presence: true
def name
"#{firstname} #{lastname}"
end
end
callsheet
型号
class Callsheet < ActiveRecord::Base
attr_accessible :crewmembers_params
has_many :castmembers
has_many :crewmembers
accepts_nested_attributes_for :crewmembers
end
callsheets
控制器
class CallsheetsController < ApplicationController
def index
@callsheets = Callsheet.all
@departments = Department.where(production_id: current_user.default_working_production_id)
end
def show
@callsheet = Callsheet.find(params[:id])
end
def new
@callsheet = Callsheet.new
@departments = Department.where(production_id: current_user.default_working_production_id)
end
def edit
@callsheet = Callsheet.find(params[:id])
end
def create
@callsheet = Callsheet.new(callsheets_params)
@Callsheet.production_id = current_user.default_working_production_id
if @callsheets.save
redirect_to callsheet_path
else
render 'new'
end
end
def update
@callsheet = Callsheet.find(params[:id])
if @callsheet.update(callsheets_params)
redirect_to callsheet_path, :notice => "callsheets successfully updated."
else
render 'edit', :notice => "callsheets not updated."
end
end
def destroy
@callsheet = Callsheet.find(params[:id])
@callsheet.destroy
redirect_to callsheets_path
end
private
def callsheets_params
params.require(:callsheet).permit(:crewmembers_params [:id, :firstname])
end
end
新表单的表单:
<%= form_for @callsheet do |f| %>
<% if @callsheet.errors.any? %>
<div id="error_explanation" class="alert alert-danger">
<strong>
<%= pluralize(@callsheet.errors.count, "error") %> prohibited
this call sheet from being saved:
</strong>
<ul>
<% @callsheet.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.fields_for :crewmember do |crewmember| %>
<fieldset>
<%= crewmember.label :firstname, "First Name" %><br />
<%= crewmember.text_field :firstname %>
</fieldset>
<% end %>
<% end %>
答案 0 :(得分:0)
您不需要attr_accessible
(仅适用于Rails 3)。
您还应该将所有模型重命名为snake_case
,引用CamelCase
:
#app/models/call_sheet.rb
class CallSheet < ActiveRecord::Base
has_many :cast_members
has_many :crew_members
accepts_nested_attributes_for :crew_members
end
与fields_for
的自定义一样,您还需要构建关联对象(如果您正在创建new
记录)(如果{{1},则不需要这样做}):
editing
这将允许您使用:
#app/controllers/call_sheets_controller.rb
class CallSheetsController < ApplicationController
before_action :set_departments
def new
@callsheet = Callsheet.new
@callsheet.crew_members.build
end
def edit
@callsheet = Callsheet.find params[:id]
end
def update
@callsheet = Callsheet.find params[:id]
@callsheet.update callsheet_params
end
private
def set_departments
@departments = Department.where(production_id: current_user.default_working_production_id)
end
def callsheet_params
params.require(:callsheet).permit(crew_members_attributes: [:id, :firstname])
end
end
-
通过<%= form_for @callsheet do |f| %>
<%= f.fields_for :crew_members do |crewmember| %>
<%= crewmember.label :firstname, "First Name" %><br />
<%= crewmember.text_field :firstname %>
<% end %>
<%= f.submit %>
<% end %>
传递嵌套属性时,需要几个组件:
fields_for
)@parent.build_child
定义fields_for
我已经概述了如何实现上述目标,所有这些都是错误的。
您还可以在同一个呼叫中声明多个验证:
parameters