ruby on rails 3 - accepts_nested_attributes_for triggering a 'Can't mass-assign protected attributes warning -
firstly first time have ever tried use accepts_nested_attributes_for
, still learning , have missed small.
so have setup , follow example doing accepts_nested_attributes_for
keep getting activemodel::massassignmentsecurity::error @ /reports can't mass-assign protected attributes: deployment
and can't figure out why, grateful code below:
report model:
class report < activerecord::base attr_accessible :name, :weekending, :visable, :deployments_attributes has_many :deployments, dependent: :destroy accepts_nested_attributes_for :deployments, allow_destroy: true end
deployments model:
class deployment < activerecord::base attr_accessible :deployment_comments, :deployment_name, :environment, :report_id belongs_to :report end
report/new.html.erb
<%= form_for (@report) |f| %> <%= render 'shared/error_messages' %> <h4>reprot detials</h4> <%= f.label "report written by: " %> <%= f.text_field :name %><br> <%= f.label "weekending: " %> <%= f.text_field :weekending %><br> <%= f.label "visable: " %> <%= f.text_field :visable %><br> <%= f.fields_for :deployment |builder| %> <%= render 'reports/forms/deployment_fields', f: builder %> <% end %><br><br> <%= f.submit class: "btn btn-large btn-primary" %> <% end %>
report/forms/_deployment_fields.html.erb
<h4>deployments</h4> <%= f.label :deployment_name, "name of deployment:" %><br /> <%= f.text_field :deployment_name %><br /> <%= f.label :environment, "environment of deployment:" %><br /> <%= f.text_field :environment %><br /> <%= f.label :deployment_comments, "deployment comments:" %><br /> <%= f.text_area :deployment_comments %><br />
reports controller:
class reportscontroller < applicationcontroller def index @reports = report.paginate(:page => params[:page], :per_page => 20, :order => 'weekending desc') end def show @report = report.find(params[:id]) end def new @report = report.new end def create @report = report.new(params[:report]) if @report.save redirect_to reports_url else render 'new' end end
db migrations:
timestamp_create_deployments.rb
class createdeployments < activerecord::migration def change create_table :deployments |t| t.string :environment t.integer :report_id t.string :deployment_name t.string :deployment_comments t.timestamps end add_index :deployments, [:report_id] end end
timestap_create_reports.rb
class createreports < activerecord::migration def change create_table :reports |t| t.date :weekending, :null => false t.string :name, :null => false t.boolean :visable, :default => false, :null => false t.timestamps end end end
so figured out, once changed
<%= f.fields_for :deployment |builder| %>
to
<%= f.fields_for :deployments |builder| %>
i found deployments fields didnt show, added
@report.deployments.build
to new method of report controller!
i'll need figure out if need update/edit method or not?
Comments
Post a Comment