2015-03-02 51 views
0

我在RegistrationsController中創建了新的after_sign_up。 它看起來很有效。
但它錯誤First argument in form cannot contain nil or be empty
而且我可以找到after_sign_upresourcenil。但在after_inactive_sign_up_path_for的資源是存在的。我怎麼能得到resource全部到after_sign_updevise的我的after_sign_up工作。但它無法加載資源


registraions_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController 
    before_filter :force_ssl! 
    before_filter :require_update_ability, only: [:edit, :update, :destroy] 

    def create 
    super # Don't fix me! 
    puts 'user is created.' 
    end 

    def after_sign_up 
    puts resource 
    end 

    private 

    def force_ssl! 
    if Rails.env.production? 
     unless request.host == 'dashboard.userhabit.io' && request.ssl? 
     redirect_to :protocol => 'https', :host => 'dashboard.userhabit.io' 
     end 
    end 
    end 

    def require_update_ability 
    unless can? :update, current_user 
     flash[:error] = I18n.t 'messages.not_authorized' 
     redirect_to apps_path 
    end 
    end 

    protected 

    def after_inactive_sign_up_path_for(resource) 
    puts resource 
    after_sign_up_path(resource) 
    end 
end 


配置/ routes.rb中

devise_for :users, :controllers => {:registrations => 'users/registrations', :sessions => 'users/sessions', :passwords => 'users/passwords'} 
devise_scope :user do 
    match 'users/after_sign_up' => 'users/registrations#after_sign_up', :as => 'after_sign_up', :via => :get 
end 


registrai附件/ after_sign_up.erb

<%= simple_form_for(resource, :as => resource_name, :url => after_sign_up_path(resource_name)) do |f| %> 

回答

0

我不知道爲什麼。但after sign_up方法使資源無。所以我資源變量進入實例類變量。但實例類變量也是零。所以我解決了全局變量。但它不乾淨。任何人有更好的主意?我粘貼我的代碼。

class Users::RegistrationsController < Devise::RegistrationsController 
    before_filter :force_ssl! 
    before_filter :require_update_ability, only: [:edit, :update, :destroy] 

    $user 

    def create 
    if after_sign_up_path(resource) 
     super 
     $user = params[:user] 
    else 
     super # Don't fix me! 
     puts 'else' 
    end 
    end 

    def after_sign_up 
    @user = $user 
    end 

    private 

    def force_ssl! 
    if Rails.env.production? 
     unless request.host == 'dashboard.userhabit.io' && request.ssl? 
     redirect_to :protocol => 'https', :host => 'dashboard.userhabit.io' 
     end 
    end 
    end 

    def require_update_ability 
    unless can? :update, current_user 
     flash[:error] = I18n.t 'messages.not_authorized' 
     redirect_to apps_path 
    end 
    end 

    protected 

    def after_inactive_sign_up_path_for(resource) 
    puts 'AfterInactiveSignUp' 
    puts resource 
    after_sign_up_path(resource) 
    end 
end