1

我提交时收到此错误,但我无法弄清楚。因为参数发送正确

在此处输入图像描述

应用控制器

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

 before_filter :configure_permitted_parameters, if: :devise_controller?

protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit! }
  end

用户.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable, :omniauthable

  validates :role, inclusion: { in: ['player', 'team', 'fan'], message: "%{value} can be player, fan or team!" }

注册页面

<div><%= f.label :role %><br />
<%= f.select :role, ['fan', 'player'].map{|r| [t(r), r]} %>

<%= f.submit "Sign up" %>

注册页面(生成)

<select id="user_role" name="user[role]"><option value="fan">Fan</option>
<option value="player">Pro Player</option></select>

服务器日志

    Started POST "/en/users" for 127.0.0.1 at 2013-07-10 23:47:35 +0200
Processing by Devise::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"6conUxB8E4y4Fy2pQ8WrT7ss/ykFqnuYzgoOkQIuSQY=", "user"=>{"email"=>"napster3000@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "role"=>"fan"}, "commit"=>"Sign up", "locale"=>"en"}
Unpermitted parameters: role
WARNING: Can't mass-assign protected attributes for User: email, password, password_confirmation

宝石文件

gem 'protected_attributes'
gem 'devise', "~> 3.0.0.rc",       github: 'plataformatec/devise' #, branch: 'rails4'
gem 'responders',          github: 'plataformatec/responders'
gem 'inherited_resources', github: 'josevalim/inherited_resources'
gem 'ransack',             github: 'ernie/ransack',            branch: 'rails-4'
gem 'activeadmin',         github: 'gregbell/active_admin', branch: 'rails4'
gem 'formtastic',          github: 'justinfrench/formtastic'
4

1 回答 1

1

为 Rails 4 设计https://github.com/plataformatec/devise/tree/rails4

gem 'devise', github: 'plataformatec/devise', branch: 'rails4'

对于其他控制器如何使属性允许更改:

例如在用户控制器中

class UsersController < ApplicationController
  def create
    @user = User.create(user_params)
  end

private
  def user_params
    params.require(:user).permit(:email, :role, ...)
  end

使用 Devise 时为用户添加自定义字段的好教程:

http://blog.12spokes.com/web-design-development/adding-custom-fields-to-your-devise-user-model-in-rails-4/

编辑

尝试覆盖方法,我认为sign_up_params应该是。registations controller

def sign_up_params
   params.require(:user).permit(:email, :password, :password_confirmation, :other, :etc)
end

关于 strong_parameters 的文档: https ://github.com/plataformatec/devise#strong-parameters

于 2013-07-10T20:46:12.933 回答