2016-01-22 182 views
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 %> 
+0

該字段爲空?你可以插入一個屏幕截圖/圖像? –

+0

電話簿/新頁面上的<%= crewmember.text_field:firstname%>'字段爲空,並且只列出一個。最終我想插入每個機組人員的姓名並允許用戶從表單中刪除機組成員。 –

+0

您是否嘗試改變<%= f.fields_for:crewmember do | crewmember | %> <%= f.fields_for:crew members | do | crewmember | %>,請注意那裏的複數。 –

回答

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記錄) (你不需要如果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通過你的控制器

我已經概述瞭如何實現上述,所有你不正確。


您還可以在同一呼叫聲明多個驗證:

#app/models/crew_member.rb 
class CrewMember < ActiveRecord::Base 
    validates :firstname, :email, presence: true 
end