2011-03-18 69 views
2

我有以下型號:軌formtastic嵌套形式警告:不能批量分配屬性保護

class CapstoneMilestone < ActiveRecord::Base 

    attr_accessible :capstone_id, :milestone_id, :rank, :id, :status, :statusweight, :rating, :ratingweight 
    belongs_to :milestone 
    belongs_to :capstone 

    accepts_nested_attributes_for :capstone, :allow_destroy => false 
    accepts_nested_attributes_for :milestone, :allow_destroy => false 


end 

class Milestone < ActiveRecord::Base 

    has_many :capstone_milestones 
    has_many :capstones, :through => :capstone_milestones 
    attr_accessible :id, :name, :description, :department_id, :project 

    accepts_nested_attributes_for :capstone_milestones, :allow_destroy => true 

end 

我也有一個formtastic形式:

<% semantic_form_for(@capstone_milestone) do |form| %> 

    <%= form.semantic_errors :state %> 
    <% form.inputs do %> 
     <%= form.input :capstone_id , :as => :select, :collection => Capstone.all %> 
     <%= form.input :milestone_id, :as => :select, :collection => Milestone.all %> 
     <%= form.input :status, :as => :numeric%> 
     <%= form.input :statusweight, :as => :numeric%> 
     <%= form.input :rating, :as => :numeric%> 
     <%= form.input :ratingweight, :as => :numeric%> 


    <% end %> 
    <%= form.inputs :name, :for => :milestone%> 

    <%= form.buttons %> 

<% end %> 

而且我capstone_milestone控制器(應)照顧更新:

def update 
    @[email protected]_milestone.milestone # I also removed these 2 lines without success 
    @milestone.update_attributes(params[:milestone_id]) 

    respond_to do |format| 
     if @capstone_milestone.update_attributes(params[:capstone_milestone]) 
     format.html { redirect_to(session[:return_to], :notice => 'Milestone was successfully updated.') } 
     format.xml { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @capstone_milestone.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

當我提交表單時,capstone_milestone更新,但是鏈接的里程碑不是。

這是服務器日誌:

Started POST "/capstone_milestones/12" for 127.0.0.1 at 2011-03-18 11:40:30 +0100 
    Processing by CapstoneMilestonesController#update as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"ZkErrLTpdp56BASPdZiiT6ZcvUx5KsI+Gm3JLnzM6D0=", "capstone_milestone"=>{"capstone_id"=>"100001", "milestone_id"=>"100002", "status"=>"80.0", "statusweight"=>"1.0", "rating"=>"", "ratingweight"=>"1.0", "milestone_attributes"=>{"name"=>"Land Control Analysis 2", "id"=>"100002"}}, "commit"=>"Update Capstone milestone", "id"=>"12"} 
    User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 LIMIT 1 
    CapstoneMilestone Load (0.4ms) SELECT `capstone_milestones`.* FROM `capstone_milestones` WHERE `capstone_milestones`.`id` = 12 ORDER BY capstone_milestones.rank LIMIT 1 
    Milestone Load (0.3ms) SELECT `milestones`.* FROM `milestones` WHERE `milestones`.`id` = 100002 LIMIT 1 
    SQL (0.1ms) BEGIN 
    SQL (0.1ms) COMMIT 
    SQL (0.1ms) BEGIN 
WARNING: Can't mass-assign protected attributes: milestone_attributes 

任何意見,建議取消阻止我將不勝感激!

回答

9

您需要添加:milestone_attributesattr_accessibleCapstoneMilestone

attr_accessible :capstone_id, :milestone_id, :rank, :id, :status, :statusweight, 
    :rating, :ratingweight, :milestone_attributes 
+0

太好了,非常感謝!尼古拉斯。 – NicdeMor 2011-03-18 12:00:47

+0

@NicdeMor很高興幫助。如果這解決了您的問題,那麼請將此答案標記爲已接受的答案:) – htanata 2011-03-18 13:38:55

相關問題