2013-06-23 113 views
-1

我知道這是一個流行的錯誤,但我有類用戶< ActiveRecord :: Base attr_protected:provider,:uid,:name,:email在我的用戶模型中,但仍然得到這個錯誤。無法批量分配受保護的屬性:電子郵件

這裏是細節:

ActiveModel::MassAssignmentSecurity::Error in UsersController#update 

Can't mass-assign protected attributes: email 
Rails.root: /Users/ewalker/Documents/alift 

Application Trace | Framework Trace | Full Trace 
app/controllers/users_controller.rb:19:in `update' 
Request 

Parameters: 

{"utf8"=>"✓", 
"_method"=>"put", 
"authenticity_token"=>"F+5itYNqPddn4usVgIJwzG+PSz50Up7mqZs50x3f9Ho=", 
"user"=>{"email"=>"[email protected]"}, 
"commit"=>"Sign in", 
"id"=>"1"} 

我的用戶控制器:

class UsersController < ApplicationController 



def show 
    @user = User.find(params[:id]) 
end 

def index 
    @users = User.all 
end 

def edit 
    @user = User.find(params[:id]) 
end 

def update 
    @user = User.find(params[:id]) 
    if @user.update_attributes(params[:user]) 
    redirect_to @user 
    else 
    render :edit 
    end 
end 
end 

用戶模型:

class User < ActiveRecord::Base 
attr_protected :provider, :uid, :name, :email 

has_many :posts, dependent: :destroy 

    def self.from_omniauth(auth) 
    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user| 
     user.provider = auth.provider 
     user.uid = auth.uid 
     user.name = auth.info.name 
     user.oauth_token = auth.credentials.token 
     user.oauth_expires_at = Time.at(auth.credentials.expires_at) 
     user.save! 
    end 
    end 
end 

和編輯形式:

<%= form_for(@user) do |f| %> 
    <%= f.label :email %> 
    <%= f.text_field :email %> 
    <br /> 
    <%= f.submit "Sign " %> 
<% end %> 

感謝

回答

4

attr_protected防止質量分配這樣的錯誤是可以預期的。 attr_accessible :email可能是你想要的,它允許在質量分配中設置屬性。

相關問題