Reputation: 1531
I am using Rails 3.2.2 and am getting the following error. The form below uses a nested model and I am unable to locate the source of the error which I have been trying to locate for days now.
ActiveModel::MassAssignmentSecurity::Error in UsersController#create
Can't mass-assign protected attributes: blog
app/controllers/users_controller.rb:17:in `new'
app/controllers/users_controller.rb:17:in `create'
Variables for form
class HomeController < ApplicationController
def index
@user = User.new
@blog = @user.blogs.build
end
end
User form
<%= form_for @user do |f| %>
<%= f.text_field :email, :class => 'textbox', :value => 'Email' %><br/><br/>
<%= f.password_field :password, :class => 'textbox', :value => 'Password' %><br/><br/>
<%= f.fields_for :blog do |b|%>
<%= b.text_field :url, :class => 'textbox', :value => 'Blog URL' %></br></br>
<% end %>
<%= image_submit_tag("signup.png") %> <br/>
<% end %>
User Controller
class UsersController < ApplicationController
def create
@user = User.new(params[:user]) ############## << RUNTIME ERROR #############
if @user.save
flash[:success] = "Welcome!"
render 'user/success'
else
render 'home/index'
end
end
Blog Model
class Blog < ActiveRecord::Base
belongs_to :user
attr_accessible :url, :type, :blog_id
validates :url, :presence => true
end
User Model
class User < ActiveRecord::Base
has_many :blogs
has_many :posts
accepts_nested_attributes_for :blogs, :allow_destroy => true
attr_accessible :email, :password, :user_id, :blogs_attributes
end
Schemas
create_table "blogs", :force => true do |t|
t.integer "user_id"
t.string "url"
t.string "type"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t|
t.string "email"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "password_digest"
t.string "remember_token"
end
Upvotes: 0
Views: 830
Reputation: 2118
Try to add an s on blogs like so. Should be blogs.
<%= f.fields_for :blogs do |b|%>
<%= b.text_field :url, :class => 'textbox', :value => 'Blog URL' %></br></br>
<% end %>
Upvotes: 1