Search code examples
ruby-on-railsruby

Renaming controllers in Rails and cleaning out generated content


I was following along with the railscast regarding the restful_authentication plugin.

He recommended running the command:

script/generate authenticated user session

Which I did, and everything generated "fine", but then sessions wouldn't work. Checking the site again, he mentions a naming standard and listed updated code which stated:

script/generate authenticated user sessions

With sessions being pluralized.

So now I have session_controller.rb with a SessionController in it, but I guess by naming standards, it is looking for SessionsController, causing the code to fail out with the error "NameError in SessionsController#create "

I see the problem, which is pretty obvious, but what I don't know is, how do I fix this without regenerating the content? Is there a way to reverse the generation process to clear out all changes made by the generation?

I tried just renaming the files to sessions_controller with e SessionsController class, but that failed.

While writing this, I solved my own problem. I had to rename session to sessions in the routes file as a map.resource and rename the view directory from session to sessions, and update session_path in the html.erb file to sessions_path.

So I solved my problem, but my answer regarding removing generated content still remains. Is it possible to ungenerate content?


Solution

  • Actually, script/destroy works for any generator - generators work by reading a script of sorts on what files to create; script/destroy just reads that script in reverse and removes all the files created, as long as you give it the same arguments you passed to script/generate.

    To sum up: script/destroy authenticated user session would have removed all the generated files for you, after which you could have run script/generate user sessions without a problem.