2013-02-02 68 views
0

我正在使用Rails 3.2.2並出現以下錯誤。下面的表格使用嵌套模型,我無法找到我一直試圖找到的錯誤的來源。ActiveModel :: MassAssignmentSecurity :: UsersController中的錯誤#創建


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' 

變量形式

class HomeController < ApplicationController 

    def index 
    @user = User.new 
    @blog = @user.blogs.build 
    end 
end 

用戶形式

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

用戶控制器

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 

博客模式

class Blog < ActiveRecord::Base 

    belongs_to :user 
    attr_accessible :url, :type, :blog_id 
    validates :url, :presence => true 

end 

用戶模型

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 

架構

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 

回答

1

嘗試在如此的博客上添加s。應該是博客。

<%= f.fields_for :blogs do |b|%> 
    <%= b.text_field :url, :class => 'textbox', :value => 'Blog URL' %></br></br> 
<% end %> 
+0

男人,總是這些小事情。有時候,這些錯誤信息可能會導致棘手的問題! ;) –

+0

我知道。我很高興我能幫上忙。 – Benjamin

相關問題