5

我是 Ruby on Rails 环境的新手,我能够解决的大多数问题,但我还没有找到解决这个问题的方法。

提供上下文:

  • 使用导轨 (4.0.0)
  • 使用守望者 (1.2.3)
  • 使用设计(3.0.0)
  • 使用 rolify (3.2.0)
  • 使用康康 (1.6.10)

我的问题

当我使用Devise 的注册注册新用户时。:name 字段未添加到数据库中。查看服务器输出

用户模型(app/models/user.rb)

:name 包含在 attr_accessible 中。

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

  # Setup accessible (or protected) attributes for your model
  attr_accessible :role_ids, :as => :admin
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me

end

用户控制器(app/controllers/users_controller.rb)。

为了与 Rails 4 保持一致,我添加了 params 白名单,但这并没有解决问题。

class UsersController < ApplicationController
  before_filter :authenticate_user!

  def index
    authorize! :index, @user, :message => 'Not authorized as an administrator.'
    @users = User.all
  end

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

  def update
    authorize! :update, @user, :message => 'Not authorized as an administrator.'
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user], :as => :admin)
      redirect_to users_path, :notice => "User updated."
    else
      redirect_to users_path, :alert => "Unable to update user."
    end
  end

  def destroy
    authorize! :destroy, @user, :message => 'Not authorized as an administrator.'
    user = User.find(params[:id])
    unless user == current_user
      user.destroy
      redirect_to users_path, :notice => "User deleted."
    else
      redirect_to users_path, :notice => "Can't delete yourself."
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:name, :email, :password, :password_confirmation, :remember_me)
    end
end

Devise 的新注册视图(app/views/devise/registrations/new.html.erb)

<h2>Sign up</h2>
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => 'form-vertical' }) do |f| %>
  <%= f.error_notification %>
  <%= display_base_errors resource %>
  <%= f.input :name, :autofocus => true %>
  <%= f.input :email, :required => true %>
  <%= f.input :password, :required => true %>
  <%= f.input :password_confirmation, :required => true %>
  <%= f.button :submit, 'Sign up', :class => 'btn-primary' %>
<% end %>
<%= render "devise/shared/links" %>

应用程序控制器(app/controllers/application_controller.rb)

我遵循了有关强参数的说明,并包含了Devise的懒人方法

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?

  rescue_from CanCan::AccessDenied do |exception|
    redirect_to root_path, :alert => exception.message
  end

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:name, :email) }
  end
end

创建新用户时的服务器输出。

Started POST "/users" for 127.0.0.1 at 2013-07-16 15:31:20 +1000
Processing by Devise::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"TYp9xOgtdKJI62rUddU7EE1C7FDF5qnmWgGENluzaWk=", "user"=>{"name"=>"John Smith", "email"=>"john.smith@example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Unpermitted parameters: name
   (0.1ms)  begin transaction
  User Exists (0.1ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = 'john.smith@example.com' LIMIT 1
Binary data inserted for `string` type on column `encrypted_password`
  SQL (0.3ms)  INSERT INTO "users" ("created_at", "email", "encrypted_password", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Tue, 16 Jul 2013 05:31:20 UTC +00:00], ["email", "john.smith@example.com"], ["encrypted_password", "$2a$10$kMfZLiBm6md0zoWXd0esjO/IRHBC72444ABDKcXVhPa6mCco9pIJu"], ["updated_at", Tue, 16 Jul 2013 05:31:20 UTC +00:00]]
   (17.0ms)  commit transaction
   (0.1ms)  begin transaction
Binary data inserted for `string` type on column `last_sign_in_ip`
Binary data inserted for `string` type on column `current_sign_in_ip`
  SQL (0.4ms)  UPDATE "users" SET "last_sign_in_at" = ?, "current_sign_in_at" = ?, "last_sign_in_ip" = ?, "current_sign_in_ip" = ?, "sign_in_count" = ?, "updated_at" = ? WHERE "users"."id" = 3  [["last_sign_in_at", Tue, 16 Jul 2013 05:31:20 UTC +00:00], ["current_sign_in_at", Tue, 16 Jul 2013 05:31:20 UTC +00:00], ["last_sign_in_ip", "127.0.0.1"], ["current_sign_in_ip", "127.0.0.1"], ["sign_in_count", 1], ["updated_at", Tue, 16 Jul 2013 05:31:20 UTC +00:00]]
   (1.1ms)  commit transaction
Redirected to http://0.0.0.0:3000/
Completed 302 Found in 94ms (ActiveRecord: 19.0ms)

我的结论

毕竟,我相信问题出在设计的registration_controller,但我不确定如何访问控制器并纠正这个问题,或者这是否是问题所在。我希望这很简单,我只是忽略了它。

如果有人遇到此问题或可以对情况有所了解,将不胜感激。

干杯。

4

4 回答 4

10

Rails 4 使用强参数。

您必须允许要通过控制器传递给模型的参数。换句话说,现在不允许通过参数直接关联到模型中的属性,您必须指定是否假设任何参数值通过控制器传递给模型。

您可能会在此博客上找到一些帮助:http: //edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html

或者好像我们考虑您的问题一样,您可以覆盖 SessionsController 进行设计或使用

devise_parameter_sanitizer(:sign_up)

更多帮助可以在这里找到:https ://github.com/plataformatec/devise#strong-parameters

于 2013-07-22T11:54:10.173 回答
8

添加

before_action :configure_permitted_parameters, if: :devise_controller?

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

到我的 application_controller 为我工作

更新

before_action :configure_permitted_parameters, if: :devise_controller?

def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:sign_in, keys: [:name, :email])
  devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :email, :password, :password_confirmation]) 
  devise_parameter_sanitizer.permit(:account_update, keys: [:name, :email, :password, :password_confirmation, :current_password])
end

看起来设计更新了他们的文档,他们希望它像上面那样设置。

于 2013-10-10T01:54:51.663 回答
2

假设您使用的是 bundle,您可以通过以下方式访问设计 gem 来查看代码:

bundle open devise

如果您需要覆盖标准设计控制器,您可以通过将控制器从 gem 复制到应用程序中的 controllers/devise/[file name] 来实现,它将优先。请注意,如果您这样做,则必须小心 gem 升级。

根据之前的评论,您为什么将白名单与强参数混合在一起;您应该只需要 rails 4 中的强大参数并设计 3

于 2013-07-16T12:26:04.463 回答
0

我在 application_controller 中发现了问题:

devise_parameter_sanitizer.for(:sign_in) ...

需要是

devise_parameter_sanitizer.for(:sign_up)...

代表我的轻微疏忽,但感谢您的帮助。

于 2013-07-22T11:21:12.583 回答