2

我已经尝试了我能找到的所有内容,包括设计自述文件 ( 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',以便我可以使用 attr_accessible 而不是强参数。使用这种方法,我似乎能够很好地保存用户,但我的任何字段,无论是自定义的还是其他的,实际上都没有保留。他们都保持为零。我还尝试将设计恢复到使用 attr_accessible 而不是强参数的先前版本,这导致了同样的问题,没有任何东西持续存在。

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

编辑:这是我的应用程序控制器:

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
4

2 回答 2

3

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

用户.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 视图

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

并在应用程序控制器中

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

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

class RegistrationsController < Devise::RegistrationsController

  def create
    raise params.inspect
  end
end

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

devise_for :users, controllers: { registrations: "registrations" }
于 2013-10-01T14:08:35.197 回答
0

您还需要为您的用户控制器添加一个强大的参数方法:

def update
  person = current_account.people.find(params[:id])
  person.update_attributes!(person_params)
  redirect_to person
end

private

def person_params
  params.require(:person).permit(:name, :age)
end
于 2013-10-02T06:03:26.990 回答