简体   繁体   中英

how use decorators in rails4?

I install gem 'devise' and make scaffold 'User'. I try extend user_controller. I create file app/decorators/controllers/users_controller_decorator.rb:

UsersController.class_eval do
  after_filter :find_all_blog_posts, :only => [:create]

  protected
    def find_all_blog_posts
      p '-------------------------------'
      binding.pry
    end
end

I run application and via browser create new user. but after press 'submit' console is not display '-------------'. This indicates that the function 'find_all_blog_posts' did not work.

Please help extend controller User in rails4.

ps:

controllers/users_controller.rb:

class UserController < ApplicationController
  load_and_authorize_resource
end

just inherit the devise userscontroller - dont override it! :-)

users_controller.rb

UsersController < Devise::UsersController

  after_filter :find_all_blog_posts, only: [:create]

  protected
  def find_all_blog_posts
    binding.pry
  end
end

routes.rb

devise_for :users, controllers:  { registrations: "users_controller" } 

and more readable stuff https://github.com/plataformatec/devise#configuring-controllers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM