3

我是 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

方案.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
4

2 回答 2

2

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

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

您需要允许设计的名称参数。在您的 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

请参阅设计 wiki上的更多信息。

于 2013-12-24T07:08:30.113 回答
0

需要进行以下更改 - 这应该可以解决它

宝石'protected_attributes'

类 RegistrationsController < Devise::RegistrationsController before_filter :configure_permitted_pa​​rameters

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

devise_parameter_sanitizer.for(:account_update) 做 |u| u.permit(:name,:user_type,:email, :password, :password_confirmation, :current_password) end end

于 2013-12-29T17:46:46.700 回答