簡體   English   中英

設計3 Rails 4不允許的參數

[英]Devise 3 Rails 4 Unpermitted parameters

這是我注冊時得到的: Unpermitted parameters: password_confirmation WARNING: Can't mass-assign protected attributes for User: email, password我已經安裝了所有內容,並且正在嘗試首次設計。 我已經創建了一個模型用戶,一切應該順利。 這是設計初次用戶注冊的全新安裝。 沒有默認代碼已被更改。 請幫忙!

解決的問題:我向user.rb模型添加了attr_accessible

檢查您的用戶模型,並查看參數是否可訪問。

請記住,您不需要命名它們,即IE我的用戶模型如下所示,帶有設計3和導軌4:

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

  validate :username, presence: true, uniqueness: true, format: { with: /[a-zA-Z0-9]{4,20}/ }
end

解決方案Rahul談論的問題沒有多大意義,因為請求中已經允許使用這些參數,但是就我而言,我為該User模型提供了一個額外的username屬性,因此我將其添加到ApplicationController中:

  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:account_update) << :username
    devise_parameter_sanitizer.for(:sign_up) << :username
  end

在您的ApplicationController中添加以下內容(如果您有Rails4)

before_filter :update_sanitized_params, if: :devise_controller?

#  method to sanitized params for devise user sign up
def update_sanitized_params
    devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:email,:password, :password_confirmation)}
end

有關更多信息,請參見https://github.com/plataformatec/devise#getting-started的 “嚴格參數”部分

如果還有其他人仍然出現此錯誤,請檢查您的表單URL路徑。 我已經將session_path復制到應該使用registration_path的表單中。

我正在本練習中首次使用Devise GEM(目前正在學習Rails)。 就我而言,我有同樣的錯誤,這是一個路由問題。

問題出在new.index.html.erb devise/registrations視圖文件夾的new.index.html.erb中的form_for上。

該表單具有url: session_path(resource_name) ,但是為了起作用,它應該指向registration_path(resource_name)

我改變了它,現在它在heroku上工作了。

是文件的鏈接。

這是heroku上的練習: devise.muga.com.co

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM