Rails 4和Devise:Devise不保存新信息(名字,姓氏,配置文件名称) [英] Rails 4 and Devise: Devise not saving new information (First Name, Last Name, Profile Name)

查看:109
本文介绍了Rails 4和Devise:Devise不保存新信息(名字,姓氏,配置文件名称)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails 4和Devise。
我正在尝试以我的应用发布的状态显示用户的名字。
我已经做了我能想到的一切(添加用户模型后迁移了数据库),并且数据库仍未保存标题中列出的三个新字段,Devise保存了它附带的内容(电子邮件+密码),但是

I am using Rails 4 and Devise. I am trying to display a user's first name in a status posted on my App. I have done everything I can think of correctly (migrated database after adding user model) and the database is still not saving the three new fields listed in my title, Devise saves the things it came with (email + password) but not the new fields.

这是我的用户。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

  # Setup accessible attributes for your model     

  attr_accessible :role_ids, :as => :admin
  attr_accessible :email, :password, :password_confirmation, :remember_me, 
                  :first_name, :last_name, :profile_name



end

我的数据库迁移:

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|

      t.string: :first_name;
      t.string: :last_name;
      t.string: :profile_name;

      ## Database authenticatable
      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.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

进行了一些研究,我认为attr_accessible与Rails 4和Devise不兼容,这是正确的吗?
如果是这样,如何将这些新信息传递到我的Devise数据库中?
有人知道这样做的好指南吗?

Having done some research, I don't think attr_accessible is compatible with Rails 4 and Devise, is this correct? If so, how do I pass this new information into my Devise db? Does anyone know a good guide to do this?

让我知道是否需要提供更多信息!

Let me know if there is any more information that needs to be provided!

预先感谢

推荐答案

为您的帮助者加油,这是我如何使它起作用的方法:

Cheers for your help guys, here's how I got it to work:

我将应用控制器更新为:

I updated my Application Controller to this:

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?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name,:last_name,:profile_name,:email, :password) }
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name,:last_name,:profile_name,:email, :password) }
  end

end

并确保将user_id添加到状态控制器的底部:

And made sure to add user_id to the bottom of your status controller:

params.require(:status).permit(:user_id, :name, :content)

此将新字段保存在您的数据库中,并允许其在状态中显示。

This will save the new fields in your db and allow it to be displayed in statuses.

这篇关于Rails 4和Devise:Devise不保存新信息(名字,姓氏,配置文件名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆