Параметры Unpermitted, добавляющие новые поля в Devise in rails 4.0
Очень новая работа с рельсами. Я внедрил базовую систему входа, используя Devise. Я пытаюсь добавить пару новых полей (bio: string, name: string) на страницу sign_up. У меня есть все, что отображается правильно, и новые поля добавляются в базу данных (когда я просматриваю его в SQLbrowser), однако они не заполняются и после того, как пользователь отправляет форму sign_up, появляется сообщение, в котором говорится:
Unpermitted parameters: bio, name
Я добавил 2 строки в _devise_create_users.rb
# added
t.string :bio
t.string :name
И у меня они появляются в schema.rb
ActiveRecord::Schema.define(version: 20130629002343) do
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 "shortbio"
t.boolean "admin", default: false
t.string "realname"
t.string "name"
t.string "bio"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
Мой user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
#:token_authenticatable, :confirmable,
#:lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
Является ли эта проблема чем-то связанным с сильными параметрами? Мне тяжело обволакивать вокруг себя и где/как реализовать.
Ответы
Ответ 1
Принятое решение достаточно хорошее, но я вижу две проблемы: 1) все контроллеры будут проверять, является ли текущий контроллер контроллером разработки (if: :devise_controller?
) и 2) нам нужно написать все приемлемые параметры в методе (...for(:sign_up) {|u| u.permit(:bio, :name)}
), даже :email
, :password
и т.д.
Я думаю, что более элегантным решением может быть:
# app/controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
before_filter :configure_permitted_parameters
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up).push(:name, :phone, :organization)
end
end
# config/routes.rb
devise_for :users, :controllers => { :registrations => "users/registrations" }
ПРИМЕЧАНИЕ. Обновления для Rails 4.2 +
Этот ответ падет устаревшим:
Ответ 2
Убедитесь, что вы используете Devise 3.0.0 по крайней мере. Добавьте к контроллеру приложения:
before_filter :update_sanitized_params, if: :devise_controller?
def update_sanitized_params
devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:bio, :name)}
end
Документация: https://github.com/plataformatec/devise#strong-parameters
Ответ 3
У меня тоже были проблемы с этим. Документация на сайте разработки помогла, а также некоторые форумы. Вот что я сделал:
В пользовательских RegistrationsController (приложение/контроллеры/users/registrations_controller.rb)
# app/controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
before_filter :update_sanitized_params, if: :devise_controller?
def update_sanitized_params
devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:name, :email, :password, :password_confirmation)}
end
end
Затем в файле маршрута (config/routes.rb) нас это для вашего заявления devise_for:
devise_for :users, controllers: {registrations: "users/registrations"}
Ответ 4
Вот еще один прямой способ, который работает в моих рельсах 4.2.1:
Создайте следующий файл
/config/initializers/devise_permitted_parameters.rb
и код..
module DevisePermittedParameters
extend ActiveSupport::Concern
included do
before_filter :configure_permitted_parameters
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :name
devise_parameter_sanitizer.for(:account_update) << :name
devise_parameter_sanitizer.for(:sign_up) << :bio
devise_parameter_sanitizer.for(:account_update) << :bio
end
end
DeviseController.send :include, DevisePermittedParameters
Ответ 5
Как для sign_up, так и для account_update сделать это для controllers/applcation_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_user!
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:password, :password_confirmation,:current_password,:email,:name, :phonenumber,:province,:city,:area,:idcardimg,:role) }
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:password, :password_confirmation,:current_password,:email,:name, :phonenumber,:province,:city,:area,:idcardimg,:role) }
end
end
Ответ 6
Проблема кажется сильными параметрами, посмотрите здесь и скопируйте код.
https://github.com/plataformatec/devise/blob/rails4/app/controllers/devise/registrations_controller.rb
Скопируйте этот файл в одно и то же место в проекте app/controllers/devise/registrations_controller.rb
и измените код действия create
# POST /resource
def create
# THIS LINE IS THE ONE YOU CHANGE
self.resource = build_resource(sign_up_params.merge(:bio, :name))
if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_up(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
respond_with resource
end
end
Я должен сказать вам, что я не очень уверен, что это работает, потому что я не использую devise, но, видя код, кажется, он будет работать.
Ответ 7
Придумайте все для этого:
В пользовательском контроллере у вас есть
private
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:full_name <add your parameter>)
end