views:

20

answers:

1

I'm building a website based on RoR, and using a third-party gem "devise". I have used rake gems:unpack to unpack the "devise" to my "vendor/gems" directory. Now, I found the method "SessionsController.create" provided by "devise" is not fit my requirement, and I want to modify it.

But I don't know what it is best way:

  1. just modify the method SessionsController.create" directly?
  2. create another SessionsController and override the "create" method?
A: 

Ruby has open classes, to you should always tend toward reopening and redefining the method(s) you need to, so reopening SessionsController and overriding the create method would be the way to go.

Editing the gem directly puts a maintenance burden on you and everyone else that has to touch the code in the future. Eventually someone is going to forget and update that gem.

x1a4
@x1a4, thanks. I also feel it is better not edit the original code directly, but I don't know how to "reopen" the SessionsController. Should I create a "sessions_controller.rb" in "app/controllers"? What will you do?
Freewind
There are a few ways of doing it, but my normal method is creating an initializer in config/initializers. Due to how Rails loads classes, adding it to app/controllers won't do what you want. If you need to get more complicated, I'd probably make a module under /lib, then include it into whatever i needed to in an initializer.
x1a4
I think it's a little strange to do it in "config/initializers", because they are controller methods. Maybe I should find some articles about "customing" devise. Your answers are still valuable, thank you
Freewind