rails scaffold form, simplest way of adding a field? - ruby-on-rails

Say I did rails g scaffold Review artist:string song:string genre:string and created a review object with those fields...would it later be possible to easily add another field like rating:int via a rails command, or would I need to manually edit all the files to include another data field for the form? I know it's possible to just delete the first scaffold and rebuild with the extra field, but I was just wondering if there was an easier way.
Thanks

The scaffold just generates minimalist model, view and controller code along with (usually) a database migration.
If you haven't made any serious changes to any of that code, it may be easier to rerun the scaffold generator. If you haven't run the database migrations, or you haven't committed them, you can add your attributes in the existing migration code and follow up with corresponding changes in the view.
If you've committed the code and someone is already depending on the existing model, you'll want to generate a database change migration to preserve a graceful upgrade/downgrade path.
The scaffold isn't magic, it's just a quick and dirty way of generating code that you can edit.

Related

Rails migration generate

How would one manage if there were a lot of migrations file made using rails g migration?
For example if I had a file that generate a column for a certain table, I had this but again later down the track I wanted to remove this. This type of adding and removing has been repeated quite a bit due to architectural decisions along the way.
I don't like the way I'm creating migrations file for every instance of column (columns) adds and I would like to clean up all my migrations file so they are simple enough.
I understand Migrations are recorded with timestamp with matching file name and in the table to check if they have been already migrated or not. I presume I can delete that specific row in question and also delete the same thing that matches with the file after deleting the column not required.
Is this the best way to go or are there any better examples of this?
It is possible to get rid of migrations to reduce the complexity and ensure that things don't get 'out of sync'.
The simplest way is to generate a new, blank migration file. Copy across the contents of schema.rband then delete all the earlier migrations.
Obviously you would want to ensure that you are using version control (such as Git) and that your database has a backup so you can revert any problems.
I wrote a blog post on the subject here http://www.fmhcc.com.au/ruby/database-migrations-in-rails-and-when-to-start-from-scratch/

Rake db:migrate and HTML generated not changing

I'm very new to RoR and building my first application. I used rails generate scaffold and created a table in database. Of course I did it wrong, wanted to make changes to the table, did it by rewriting the generated migration file. I think rake db:migrate works fine, because it's updating my schema, but there are no changes visible on the site. The view of the table didn't change, although the mechanism is different, I can't add anything now because it can't find the proper columns after I changed them. I have no idea how to fix it without rewriting the view files myself. Is it possible, I think I'm missing something?
You can undo/destroy scaffold by rails destroy scaffold scaffold_name and then re-generate scaffold with the columns you added on migration file. FYI, editing migration files is not a good practice.
You have two options, either you destroy the scaffolding that you've created with rails destroy scaffold ModelName or you change the views by hand. When you run rake db:migrate it won't do anything in the views.

Is there a built in way to rename an entire scaffold?

I'm using Rails 3.2, and have started with a scaffolding and built out from it, but have realized I need to rename the entire scaffold (Model, View, Controller, db:migrate, etc). Is there a built in way to do this, or should I just do it manually?
I don't think there's anything rails provides to rename the name of models/controllers/views/tests etc. once they are created - whether as a part of a scaffold, or not.
You will have to change it manually.
If it is a brand new app that you have just started on, it might be easier to just delete the whole directory/drop the database, and start over again.
If not, you will have to go through the files created/modified by the scaffold generation, and modify them manually.
Make sure you either drop_and_recreate the relevant table, or add a migration to rename the table. See How do you write a migration to rename an ActiveRecord model and its table in Rails? for some relevant advice.
I think there's no out of the box method to rename files generated by scaffold.What you have to do is create a new scaffold and copy your codes from the old to the new.Copy contents from factories, model, controller and their respective spec file to the new scaffold. Then remove the old scaffold with the command
rspec d scaffold <Model Name>
and you have to create a new migration to drop that old table from the database. Then run your migrations.

Rails: renaming a controller and corresponding model

Is there an easy way to rename a controller and model in my app and all the instances in the corresponding code?
I'm using textmate, would this be as simple as using the replace function and replacing the word Post with Report?
You need to change the name of the Controller and the associated Model, Views, Helpers, Tests & Routes directories, file names, class names & the names in the class definitions.
I found two ways to do this but before you try anything I recommend you back-up your app, preferably with a Software Version Control system like Git & Github.com.
Your first option is to do it manually & there is a good explanation on how to do this here: How to rename rails controller and model in a project
Another way is to destroy your controller & model, and then generate a new one, this will remove all the files which were generated the first time round & replace them with new ones. Michael Hartl explains this solution well in his online guide to Ruby on Rails here: http://ruby.railstutorial.org/chapters/static-pages#sidebar-undoing_things
This is the solution I followed when I needed to make this change to my app, I needed to replace a MVC scaffold I generated called board with a new one called product.
1. First
I made a back-up of the work I did in the layout of the board view, app/views/boards/index.html.erb
2. Then
I ran the below rails commands in the terminal window.
$ rake db:rollback
$ rails destroy scaffold board name:string description:text image:string price:decimal
$ rails generate scaffold product product_type:string name:string description:text image:string price:decimal
$ rake db:migrate
3. Finally
I copied my backed-up boards/index.html.erb file into the newly generated app/views/products/index.html.erb & did a find & replace in my text editor on this file to replace board with product.
I think the second option is much more reliable & quicker but it’s important to make this change early on in your project before you make too many manual changes to the code. It would be better to just take a little more time planning your MVC names & database tables properly before you start your project.
You can also use rails_refactor gem to rename controller, model, etc
for more info check https://github.com/jcrisp/rails_refactor
To rename controller and model use this gem https://github.com/jcrisp/rails_refactor
If you are using textmate, use 'command-shift-f" to look for a string throughout your entire project.
Yes and no. You can rename it that way, but you'll also need to rename the files as well or Rails won't know where to look for the files corresponding to the new Report model/controller/etc.

Re-Generating Scaffold

Is there any way to re-generate views from newly updated model?
Unless you've made a lot of changes to the scaffold, it's probably easier just to overwrite the current scaffold:
./script/generate -f scaffold Model
I had the same need, I just backed up some changes and did a destroy/generate, Take care, destroy deletes Everything related to the Model :
rails destroy scaffold Model -f
before I generate again.
rails generate scaffold Model
The scaffold is not intended to create the interface, only to make a starting point for further development. I'm not sure it's even possible (without a lot of unnecessary work) to redo a scaffold, it's much quicker to manually edit the view and controller anyway.

Resources