2014-10-07 61 views
0

我想在我的應用中使用Devise Invitable實現對現有用戶的邀請。update_resource_params給出不允許的參數錯誤 - 設計應用

乍一看這失敗了,因爲Devise Invitable最適用於新用戶 - 即未註冊。

,但是這是我的User::InvitationsController樣子(截斷簡潔):

class Users::InvitationsController < Devise::InvitationsController 
include ApplicationHelper 
    before_filter :configure_permitted_parameters, if: :devise_controller? 
    before_filter :update_sanitized_params, only: :update 

    # PUT /resource/invitation 

    def create 
    invited_user = User.where(email: params[:user][:email]) 
    if !invited_user.empty? 
     invitation_token = Devise.token_generator.digest(resource_class, :invitation_token, update_resource_params[:invitation_token]) 
     self.resource = resource_class.where(invitation_token: invitation_token).first 
     family_tree = self.resource.invited_by.family_tree 
     family_tree.memberships.create(:user_id => user.id, relation: update_resource_params[:relation]) 
     resource.create_membership_both_ways(params[:user][:invitation_token], params[:user][:relation]) 
     resource.skip_password = true 
     resource.update_attributes update_resource_params.except(:invitation_token) 
     redirect_to my_tree_path 
    else 
     super 
    end 
    end 

    protected 

    def update_sanitized_params 
    devise_parameter_sanitizer.for(:accept_invitation) do |u| 
     u.permit(:name, :password, :password_confirmation, :invitation_token, :invitation_relation,:avatar, :avatar_cache, :relation) 
    end 
    end 

    def update_resource_params 
    devise_parameter_sanitizer.sanitize(:accept_invitation) do |u| 
     u.permit(:email) 
    end 
    end 
end 

當我使用撬進行調試,這就是發生在我閒逛invitation_token

[1] pry(#<Users::InvitationsController>)> invitation_token 
=> false 
[2] pry(#<Users::InvitationsController>)> update_resource_params 
Unpermitted parameters: email 
=> {"name"=>"", "invitation_relation"=>"uncle"} 

思考關於什麼可能導致這種情況,或者我如何擺脫這種unpermitted paramters :email問題?

編輯1

這些都是相關的路線:

devise_for :users, :controllers => { :invitations => 'users/invitations', :confirmations => 'confirmations' } 

    devise_scope :user do 
    post "users/invitation/sign_in" => "users/invitations#invite_sign_in" 
    end 

編輯2

在我application_controller.rb我有我添加:email的方法,它似乎已停止那個錯誤:

def configure_permitted_parameters 
    # Once I added :email to this method, it stopped throwing the unpermitted error 
    devise_parameter_sanitizer.for(:accept_invitation) do |u| 
     u.permit(:name, :email, :last_name, :invitation_relation) 
    end 
    end 
+0

IM一個假設你有這樣的事情這在你的路線:controllers => {:invitations =>「invitations」} – Richlewis 2014-10-07 07:04:36

+0

也這樣做的幫助https://github.com/scambra/devise_invitable/wiki/Devise-Invitable-with-Strong-Parameters – Richlewis 2014-10-07 07:10:18

+0

@Richlewis我更新與我的路線文件信息的問題,但你是對的。我在我的路線文件中有。重新鏈接...所以確切的代碼片段根本沒有幫助 - 只是試了一下。至少,方法名稱是不同的,所以除非'update_resource_params'調用'resource_params',否則我看不出它會有什麼幫助。除此之外,它似乎是我在那裏的Rails 3版本,但功能不那麼強大。我錯了嗎? – marcamillion 2014-10-07 08:30:34

回答

1

當使用設計和配置permitted_pa​​rams最好是做到這一點的應用控制器, 你可以做到這一點的兩種方式

def configure_permitted_parameters 
devise_parameter_sanitizer.for(:accept_invitation) do |u| 
    u.permit(:name, :email, :last_name, :invitation_relation) 
end 
end 

OR

def configure_permitted_params 
    devise_parameter_sanitizer.for(:accept_invitation) << [:name, :email, :last_name, :invitation_relation] 
end 
+0

太棒了。大。 – marcamillion 2014-10-09 21:04:55

+0

該解決方案對我無效 – Extazystas 2016-01-22 12:22:49

相關問題