ruby-on-rails - 无法使用强参数将自定义字段添加到 Rails 4 中的 Devise 用户模型

标签 ruby-on-rails ruby devise ruby-on-rails-4 attr-accessible

我已经尝试了我能找到的所有内容,包括 Devise 自述文件 ( https://github.com/plataformatec/devise#strong-parameters ) 中的内容、对自述文件 ( http://blog.12spokes.com/web-design-development/adding-custom-fields-to-your-devise-user-model-in-rails-4/comment-page-1/#comment-26217 ) 的更具描述性的看法,并且我在 StackOverflow 上找到的每个答案似乎都与这两个一致解决方案。所有这些解决方案都会导致相同的错误: 2 个错误禁止保存该用户: - 邮箱不能为空 - 密码不能为空

我还尝试在 gemfile 中包含 gem 'protected_attributes' 并运行 bundle ,以便我可以使用 attr_accessible 而不是强参数。通过这种方法,我似乎能够很好地保存用户,但我的任何字段(无论是自定义字段还是其他字段)实际上都没有保留。它们全部为零。我还尝试将设备恢复到以前的版本,其中使用 attr_accessible 而不是强参数,这导致了同样的问题,但没有任何内容持续存在。

我目前对此完全处于停滞状态,因此任何有关其他可能有效的解决方案的想法将不胜感激......提前致谢!

编辑:这是我的应用程序 Controller :

class ApplicationController < ActionController::Base
  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
         devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:profile_name, :email, :password) }
         devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :profile_name, :email, :password, :password_confirmation) }
         devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :profile_name, :email, :password, :password_confirmation, :current_password) }
  end


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

end

最佳答案

我创建了一个示例应用程序,这对我有用。

用户.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

在设备注册new.html.erb View 中

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :firstname %><br />
  <%= f.text_field :firstname, :autofocus => true %></div>

  <div><%= f.label :lastname %><br />
  <%= f.text_field :lastname %></div>

  <div><%= f.label :email %><br />
  <%= f.email_field :email, :autofocus => true %></div>

  <div><%= f.label :password %><br />
  <%= f.password_field :password %></div>

  <div><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></div>

  <div><%= f.submit "Sign up" %></div>
<% end %>

<%= render "devise/shared/links" %>

以及在 devise_create_user 迁移中

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|
      ## Database authenticatable
      t.string :firstname
      t.string :lastname
      t.string :email,              :null => false, :default => ""
      t.string :encrypted_password, :null => false, :default => ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, :default => 0, :null => false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, :default => 0, :null => false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at

      t.string :authentication_token


      t.timestamps
    end

    add_index :users, :email,                :unique => true
    add_index :users, :reset_password_token, :unique => true
    # add_index :users, :confirmation_token,   :unique => true
    # add_index :users, :unlock_token,         :unique => true
  end
end

在路线

Topic::Application.routes.draw do

  devise_for :users#, controllers: { registrations: "registrations" }

  resources :maintopics do
    resources :subtopics
  end
end

在应用程序 Controller 中

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

  before_filter :configure_permitted_parameters, if: :devise_controller?

  def configure_permitted_parameters
   devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:firstname, :lastname, :email, :password, :password_confirmation) }
  end

  protect_from_forgery with: :exception

end

在您的情况下,我认为存在参数问题,因此创建新的注册 Controller 并在其中添加以下代码来检查参数值。

class RegistrationsController < Devise::RegistrationsController

  def create
    raise params.inspect
  end
end

在路由文件中用以下行替换现有的。

devise_for :users, controllers: { registrations: "registrations" }

关于ruby-on-rails - 无法使用强参数将自定义字段添加到 Rails 4 中的 Devise 用户模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19116653/

相关文章:

ruby-on-rails - 我如何运行 rake db :migrate in automated deploy with heroku or heroku-api gem?

ruby-on-rails - 在注册页面前添加条件?

ruby-on-rails - 设计:创建没有密码的用户

ruby-on-rails - rails 3 : use contents of an array as variables in a where method

ruby-on-rails - 将方法添加到 Rails 中的模型时出现未定义的方法错误

javascript - Ajax 调用 onClick 创建模型实例 (Rails 4)

Ruby:定义类变量时 class_exec 出现意外结果

ruby-on-rails - 检查字符串是否在 Rails 中全部大写

ruby-on-rails - 如何通过 Ajax 请求处理 Rail 的 flash?

ruby-on-rails-3 - 设计可重新确认