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

If you are using rails4 you need strong parameters. If you need to implement extra parameters to devise, you could either create an additional 1-1 relation called profiles for users and store them there (better in the long run of app or if you have more user data) and I personally feel it to be much easier the incorporating your user data in the devise user table.

Alternatively you could do the following if you are using one or two attributes.

You need to permit the name parameter for devise. in your ApplicationController code, do this.

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

Refer more on this on devise wiki.

于 2013-12-24T07:08:30.113 に答える
0

次の変更を行う必要があります-これで修正されるはずです

宝石 'protected_attributes'

class RegistrationsController < Devise::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) end

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

于 2013-12-29T17:46:46.700 に答える