开发者

rails formtastic nested form WARNING: Can't mass-assign protected attributes

I've following models:

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

I also have a formtastic form:

<% 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 %>

And my capstone_milestone contr开发者_JAVA技巧oller (should) take care of the update:

  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

When I submit the form, the capstone_milestone is update but the linked milestone is not.

This is the server log:

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

Any idea, suggestion to unblock me would be greatly appreciated!


You need to add :milestone_attributes to attr_accessible of CapstoneMilestone.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜