3
我正在嘗試使用fields_for
並創建一個嵌套表單,但只有一個文本字段顯示爲空白。我有3 crewmember
記錄。Rails fields_for not working
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
形式爲新callsheet:
<%= 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 %>
該字段爲空?你可以插入一個屏幕截圖/圖像? –
電話簿/新頁面上的<%= crewmember.text_field:firstname%>'字段爲空,並且只列出一個。最終我想插入每個機組人員的姓名並允許用戶從表單中刪除機組成員。 –
您是否嘗試改變<%= f.fields_for:crewmember do | crewmember | %> <%= f.fields_for:crew members | do | crewmember | %>,請注意那裏的複數。 –