为什么Devise一直问我这个名字不能是空白的?

时间:2013-06-22 23:57:40

标签: ruby-on-rails ruby forms authentication devise

我是Rails的新手,我正在与Devise合作。我面临的问题是我的表单在注册时没有更新用户的名称列。我在数据表中有name属性,但我似乎无法使用设计注册中的表单来更改它。

<h2>Sign up</h2>

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

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

  <div><%= f.label :email %><br />
  <%= f.email_field :email %></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" %>

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

  end

Scheme.rb

ActiveRecord::Schema.define(version: 20130622203624) do

  create_table "posts", force: true do |t|
    t.integer  "user_id"
    t.text     "description"
    t.integer  "comments_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "title"
  end

  create_table "users", force: true do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "name",                                null: false
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

  create_table "views", force: true do |t|
    t.string   "email",                              default: "", null: false
    t.string   "encrypted_password",     limit: 128, default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",                      default: 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "views", ["email"], name: "index_views_on_email", unique: true, using: :btree
  add_index "views", ["reset_password_token"], name: "index_views_on_reset_password_token", unique: true, using: :btree

end

2 个答案:

答案 0 :(得分:2)

如果您使用的是rails4,则需要强大的参数。如果你需要实现额外的参数来设计,你可以为用户创建一个额外的1-1关系称为配置文件并将它们存储在那里(从长远来看,或者如果你有更多的用户数据,那就更好了)我个人觉得它将您的用户数据合并到设计用户表中要容易得多。

如果您使用一个或两个属性,则可以执行以下操作。

您需要为设计允许name参数。在您的ApplicationController代码中,执行此操作。

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

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :name
   end
end

devise wiki上详细说明。

答案 1 :(得分:0)

需要进行以下更改 - 并且应该修复它

gem&#39; protected_attributes&#39;

class RegistrationsController&lt;设计:: RegistrationsController before_filter:configure_permitted_pa​​rameters

def configure_permitted_pa​​rameters  devise_parameter_sanitizer.for(:sign_up)do | u |   u.permit(:name,:user_type,:email,:password,:password_confirmation)  端

devise_parameter_sanitizer.for(:account_update)do | u |   u.permit(:name,:user_type,:email,:password,:password_confirmation,:current_password)  结束 端