2013-11-02 44 views
0

试验Devise 3.0 for Rails 4并考虑集成到现有应用程序中以取代我自己的身份验证系统。覆盖设计控制器

我自己的users_controller在注册期间与API(条纹)进行通信,我不确定如果我使用Devise如何包含此功能?

我想以某种方式覆盖/扩展设计控制器?任何帮助将非常感激。

回答

1

您可以在控制器中定义动作并在其中调用super,其中将包含相应设计动作的功能。你也可以添加你自己的功能。

例如:考虑中的注册控制器(Registrations Controller

在这里,你自己创建登记控制器的作用,并在代码注释添加功能,你可以使用登记控制器的色器件的创建行动的代码。

class RegistrationsController < Devise::RegistrationsController 
    . 
    . 
    . 

    def create 
    build_resource(sign_up_params) 

    if resource.save 
     if resource.active_for_authentication? 
     set_flash_message :notice, :signed_up if is_navigational_format? 
     sign_up(resource_name, resource) 
     # Here the user successfully signs up, so you can use your stripe API calls here. 
     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 

end 
+0

感谢 - 但我如何检测是否用户注册成功了吗? – tommyd456

+0

所以你问的是注册控制器的创建动作。 –

+0

是的 - 在创建操作中,如果用户成功创建,那么我想与Stripe API进行通信 - 我还需要更新新创建的用户的属性! – tommyd456

0

对于未来的Google:它是可以调用super在继承控制器与块的动作,做任何你想要的,而不会覆盖设计代码:

class Users::RegistrationsController < Devise::RegistrationsController 
    def create 
    super do |user| 
     NotificationMailer.user_created(user).deliver 
    end 
    end 
end