2015-12-02 65 views
0

我正在使用Devise Invitable,我在SO(here is one)上發現了幾個答案,這些答案表明我應該在我的Users::InvitationsController < DeviseController中指定動作after_invite_path_for(resource)如何在發送邀請後更改重定向路徑?

我試過了,它仍然不起作用。

這是我的控制器:

class Users::InvitationsController < Devise::InvitationsController 

    def after_invite_path_for(resource) 
    root_path 
    end 

    def create 
     @member ||= Member.find_or_create_by(email: params[:user][:email], first_name: get_first_name(params[:user][:full_name]), last_name: get_last_name(params[:user][:full_name]), bio: params[:user][:bio], gender: params[:user][:gender]) 
     @membership ||= Membership.find_or_create_by(member: @member, family_tree: current_user.family_tree, inviter: current_user, relation: params[:user][:invitation_relation]) 

     if @user_exists ||= invited_user_exists(params[:email]) 

     else 
     super 
     end 

    end 
end 

當我嘗試只需添加一個redirect_to root_pathelsesuper之後....這將引發約重定向錯誤已經被調用。

我認爲這是事實,因爲如果您檢查create action here的來源,您將看到重定向。

這是服務器日誌:

Started POST "https://stackoverflow.com/users/invitation" for 127.0.0.1 at 2015-12-02 03:58:57 -0500 
Processing by Users::InvitationsController#create as HTML 
    Rendered users/mailer/invitation_instructions.html.erb within layouts/mail (1.2ms) 

    # truncated for brevity 

    Rendered users/invitations/create.html.erb within layouts/devise (0.6ms) 
    Rendered shared/_navbar.html.erb (1424.4ms) 
Completed 200 OK in 9221ms (Views: 4732.1ms | ActiveRecord: 102.0ms) 

注意,它從來沒有在任何地方重定向。

+0

可能重複的[Rails設計發送邀請後的Invitable重定向](http://stackoverflow.com/questions/12720612/rails-devise-invitable-redirect-after-send-invititaion) –

+0

@PardeepDhingra這是問題和回答我在我的問題中提到。我的問題的整個前提是基於另一個不起作用的事實。 – marcamillion

回答

0

我想通了。無論出於何種原因,在我的Users::InvitationsController中執行after_invite_path_for(resource)並不是覆蓋DeviseInvitable#Create中的create操作。

所以,我所做的只是使用官方創建操作的大部分邏輯,並且只是在本地更改了重定向。

所以現在,我在我的控制器創建操作是這樣的:

def create 
    @member ||= Member.find_or_create_by(email: params[:user][:email], first_name: get_first_name(params[:user][:full_name]), last_name: get_last_name(params[:user][:full_name]), bio: params[:user][:bio], gender: params[:user][:gender]) 
    @membership ||= Membership.find_or_create_by(member: @member, family_tree: current_user.family_tree, inviter: current_user, relation: params[:user][:invitation_relation]) 

    if @user_exists ||= invited_user_exists(params[:email]) 

    else 
    self.resource = invite_resource 
    resource_invited = resource.errors.empty? 

    yield resource if block_given? 

    if resource_invited 
     if is_flashing_format? && self.resource.invitation_sent_at 
     set_flash_message :notice, :send_instructions, :email => self.resource.email 
     end 
     redirect_to root_path, notice: "Invitation Successfully Sent." 
    else 
     respond_with_navigational(resource) { render :new } 
    end 
    end 

end 

工程。這不是Devise慣用的......但它適用於我所需要的。