rails formtastic嵌套表单警告:无法批量分配受保护的属性

时间:2011-03-18 10:48:23

标签: ruby-on-rails 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

我也有一个形式:

<% 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
    @milestone=@capstone_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

任何想法,建议取消阻止我将不胜感激!

1 个答案:

答案 0 :(得分:9)

您需要将:milestone_attributes添加到attr_accessible的{​​{1}}。

CapstoneMilestone