Question

I'm baffled with forms for nested attributes

Here's my model:

class User < ActiveRecord::Base

  has_one :user_email_preference, :dependent => :destroy
  accepts_nested_attributes_for :user_email_preference
  attr_accessible :user_email_preference_attributes

Here's my view with devise:

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
  <%= f.fields_for resource.user_email_preference do |email| %>
  <li> 
    <h3><%= email.label :alerts %></h3>
    <div><%= email.check_box :alerts, class:"js-switch" %></div>
  </li>
  <li> 
    <h3><%= email.label :newsletter %></h3>
    <div><%= email.check_box :newsletter, class:"js-switch" %></div>
  </li> 
  <% end %>
<% end %>

The view renders correctly with http get. However submitting the form constantly gives me the following error message:

Can't mass-assign protected attributes: user_email_preference

Any suggestions on where went wrong?

Was it helpful?

Solution

It should just be f.fields_for :user_email_preference do |email|. This will set the correct nested attribute name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top