Rails will not let me destroy scaffolding from nifty generator - ruby-on-rails

I was following one of the railscasts tutorials and decided to install nifty generators. Well, being a rails noob I didn't realize that the way parameters are handled changed. Now I can't undo any of my changes. So far I managed to roll back the database but every time I try to run
rails destroy nifty:scaffold mymodel
I get the error message
attr_accessible is extracted out of rails into a gem. Please use new recommended protection model for params(strong_parameters) or add protected_attributes to your Gemfile to use old one.
So I did. I added
gem 'protected_attributes'
and ran
bundle install
Then I tried to destroy it and it errored out again. I really hope nifty didn't just screw up my project. Can anyone help?

Um, this not a real solution, but a possible workaround: if the output of the rails generate nifty:scaffold mymodel command is still in your terminal buffer, you could manually delete the files it created.
And if the output isn't available, you could do rails generate nifty:scaffold mymodel2 in order to see what files nifty:scaffold created before manually deleting the corresponding files for mymodel.
Not elegant, but it might get you over the hump.

Related

Where can I look at the Rails controller high-level superclasses?

I am trying to understand some of the higher level functioning of Rails, using the Rails console. I run controller.class.superclass.superclass which gives ActionController::Base, controller.class.superclass.superclass.superclass which gives ActionController::Metal and controller.class.superclass.superclass.superclass.superclass gives AbstractController::Base.
I have found these in the API documentation.
http://api.rubyonrails.org/classes/AbstractController/Base.html
http://api.rubyonrails.org/classes/ActionController/Metal.html
[can only post two links]
I can add to these simply by declaring the classes again in the console, but is there a way to find the original Ruby code for these and to inspect and edit it in its original file(s)? Just in case I need to know the full contents of these for future.
You can also do:
bundle show <gem>
and that will show you where the gem is on your system. Editing in those files is not advised unless you know how to re-install gems.
You can see the Rails source code on Github:
https://github.com/rails/rails/tree/master/actionpack/lib/action_controller

Rails 3.1 migrations drops timestamp in migration filenames

I am trying to upgrade an application from Rails 3.0.7 to 3.1.1. When I try to run a migration under 3.1.1, the migration file gets generated but without the leading timestamp. For example:
$ rails g migration add_foo_to_users foo:boolean
invoke active_record
create db/migrate/_add_foo_to_users.rb
The contents of the generated file look correct (with the new cool change method), but the migration file seems like it should be named more like db/migrate/20111012002448_add_foo_to_users.rb.
I suspect that I am missing something simple, but spent all weekend trying to figure out what it is. Search shows nothing helpful that I found. Migrations have been working all along before this, so something in the steps I took to upgrade seems to have broken it. Any suggestions of where to look would be appreciated. Thanks.
I have traced this issue down and fixed it in the GateGuru fork of also_migrate. A pull request is pending for merge back into also_migrate.
For anyone bitten by this in the interim, the pull request is at: https://github.com/winton/also_migrate/pull/10
You can also install from my fork via Bundler to fix the issue:
gem 'also_migrate', '0.3.5', :git => 'git#github.com:GateGuru/also_migrate.git', :branch => '8-fix-rails-3_1-timestamps'
Just in case anyone ends up here after hitting the same problem. After copying the entire application to another directory, and comparing to a clean install of a new Rails application, I narrowed this down to the configuration block for the also_migrate gem. When I comment out the lines for this configuration, the migration files were generated properly. Uncommenting the same lines recreates the error.

Generate only tests from existing model / controllers

I have a Rails3 app that is based on someone else's work. For some reason they decided not to supply the tests with the app, which I am finding frustrating.
What I want to be able to do is scaffold the tests for all the existing controllers and models so I can get a head start on creating the tests myself in test::unit. I don't want to recreate the models or controllers, just create the tests.
I am new to Rails, and have hunted about for the rake command that might do this, but all with no luck so far. Any advice / direction most appreciated.
I know it's a little old, but you can do this:
rails g scaffold Post -s
The -s makes it skip the files already created. Also, if you don't use the flag it just asks you if you want to override the file, so, no worries.
To only generate the associated test files for an existing Rails 3 app, I use "generate resource" but skip everything that I don't want:
rails g resource Post --skip --no-resource-route --no-migration --no-helper --no-assets
Other options can be found using rails generate resource --help
-s, [--skip] # Skip files that already exist
--resource-route # Indicates when to generate resource route
[--helper] # Indicates when to generate helper
[--assets] # Indicates when to generate assets
[--migration] # Indicates when to generate migration
Why not use generate scaffold? Because it might generate views that I'm not using.
There's no way to do this that I'm aware of. It would be pretty easy though to just create a temporary rails project and generate scaffolds for all of your models then copy the resulting test directory into the real project.
I.e.
rails new temporary
cd temporary
rails g scaffold Post title:string body:text
rails g scaffold Comment post:references author:string body:text
cp -r test ../real_rails_app/
etc.
This answer is now out of date. Up to date rails versions allow you to generate only the missing files with the skip option.

Adding a hook to script/generate migration

I do my Rails dev from xterm and in vim.
I'm getting sick of running script/generate migration do_whatever, then trawling through db/migrate trying to tab-complete to 20091015235018_do_whatever when there's 5 other migrations with similar timestamps.
What's the best way to add a hook to open the generated migration in vim?
I'd rather not hack into Rails' core in /usr/lib as I work from several systems and can see myself wanting to create numerous such hooks. However, it doesn't really seem plugin worthy.
There are lots of simple ways to get the name of the generated file, but I'm not sure how to cleanly hook it into the generation. What do you think?
A better option with vim is to use rails.vim and type :Rmigration do<TAB>
It ignores the timestamp ...
You can also create and edit a migration in one go by typing :Rgenerate migration ...
I just submitted a patch to Rails to add an --editor option for just this case, which you could backport into whatever version of Rails you're using now. If you don't specify which editor you want it tries to use EDITOR

Getting tables to work is RadRails?

When I manually enter tables into the .rb file in the migrate folder nothing shows up. I have followed a few different "how to's" but I can't get the tables to show up when I preview my work. I can however get one table to show up if I create it in the parameters when generating a scaffold, but that's one table. I have Rails version 2.3.4 and version 1.2.3 installed on my lame Windows Vista. I have tried using both versions multiple times. I am also trying to follow the Ruby on Rails for Dummies book, but it seems a little outdated. Any tips on getting tables to work? Its supposed to be really simple and take "just minutes" to complete this simple app. Maybe something didn't install right? Maybe a better book?
Just want to make sure you're using all the tools available to you:
To generate a migration file:
./script/generate MigrationFileName
Take a look at the Database Migrations Guide from the official Rails Guides for the correct syntax and options for creating a migration file.
Once you've created your migration, you need to run the following:
rake db:migrate
This will populate the database with the information specified in the migration file.

Resources