2012-09-12 84 views
3

我有一段時間試圖讓這個嵌套模型工作。我嘗試了所有的多元化/單數形式,完全除去attr_accessible,誰知道還有什麼。「無法批量分配受保護的屬性」與嵌套的受保護模型

restaurant.rb:

# == RESTAURANT MODEL 
# 
# Table name: restaurants 
# 
# id   :integer   not null, primary key 
# name  :string(255) 
# created_at :datetime   not null 
# updated_at :datetime   not null 
# 

class Restaurant < ActiveRecord::Base 
    attr_accessible :name, :job_attributes 

    has_many :jobs 
    has_many :users, :through => :jobs 
    has_many :positions 

    accepts_nested_attributes_for :jobs, :allow_destroy => true 

    validates :name, presence: true 

end 

job.rb:

# == JOB MODEL 
# 
# Table name: jobs 
# 
# id   :integer   not null, primary key 
# restaurant_id :integer 
# shortname  :string(255) 
# user_id  :integer 
# created_at :datetime   not null 
# updated_at :datetime   not null 
# 

class Job < ActiveRecord::Base 
    attr_accessible :restaurant_id, :shortname, :user_id 

    belongs_to :user 
    belongs_to :restaurant 
    has_many  :shifts 


    validates :name, presence: false 

end 

restaurants_controller.rb:

class RestaurantsController < ApplicationController 

    before_filter :logged_in, only: [:new_restaurant] 

    def new 
    @restaurant = Restaurant.new 
    @user = current_user 
    end 

    def create 
    @restaurant = Restaurant.new(params[:restaurant]) 
    if @restaurant.save 
     flash[:success] = "Restaurant created." 
     redirect_to welcome_path 
    end 
    end 

end 

new.html.erb:

<% provide(:title, 'Restaurant') %> 

    <%= form_for @restaurant do |f| %> 
     <%= render 'shared/error_messages' %> 

     <%= f.label "Restaurant Name" %> 
     <%= f.text_field :name %> 

     <%= f.fields_for :job do |child_f| %> 

       <%= child_f.label "Nickname" %> 
       <%= child_f.text_field :shortname %> 

     <% end %> 

     <%= f.submit "Done", class: "btn btn-large btn-primary" %> 

    <% end %> 

輸出參數:

{"utf8"=>"✓", 
"authenticity_token"=>"DjYvwkJeUhO06ds7bqshHsctS1M/Dth08rLlP2yQ7O0=", 
"restaurant"=>{"name"=>"The Pink Door", 
"job"=>{"shortname"=>"PD"}}, 
"commit"=>"Done"} 

我收到的錯誤是:

ActiveModel::MassAssignmentSecurity::Error in RestaurantsController#create 

Cant mass-assign protected attributes: job 
Rails.root: /home/johnnyfive/Dropbox/Projects/sa 

Application Trace | Framework Trace | Full Trace 
app/controllers/restaurants_controller.rb:11:in `new' 
app/controllers/restaurants_controller.rb:11:in `create' 

任何人有任何線索如何得到這個工作?謝謝!

+1

與你的問題無關 - 我注意到每個類定義數據庫結構頂部的註釋。這是一個不斷更新的痛苦,並且很容易變得陳舊。當您運行遷移時,Rails會很好地保持schema.rb的最新狀態,並且可能會更好地將數據庫記錄到您的模型中。 –

+1

@MichaelShimmins我不這麼做,它是由gem'annotate'完成的。 – Morgan

回答

3
在restaurant.rb

應該 attr_accessible :name, :jobs_attributes 而不是 attr_accessible :name, :job_attributes

+0

謝謝你(不太確定),但是我在當前的迭代中有這樣的情況,而且我仍然收到同樣的錯誤。 – Morgan

+1

餐廳模特有很多工作,所以我想你應該把'<%= f.fields_for:jobs do | child_f | %>'而不是':job' –

+0

@Kishie,我同意你的觀點,並且已經嘗試過,但是當我這樣做時,在瀏覽器中查看'shortname'字段完全消失。我確信這簡直是愚蠢的,但我無法想象出我的生活。 – Morgan

0

關於你的最後的評論: 您可以將您的工作表單內提交USER_ID,應提交USER_ID到模型

相關問題