render 'form' not showing columns added using a migration - ruby-on-rails

Initially I ran rails g scaffold Post title:string content:text and after migrating everything worked fine.
However, later on I decided to add a tags column, so I did rails g migration AddTagsToPosts tags:string and rake db:migrate.
Although the output from those commands seemed normal, the /posts/new page still only shows the initial two fields, title and content. The same goes for /posts/x/show and /posts/x/edit.
The view file itself only contains render 'form', so apparently I missed something here?
Do I have to run any additional commands to add the new fields to the forms?
Just starting to get into Rails, and this particular issue is pretty hard to google so I figured I'd ask the question here.

Adding the migration doesn't change the views that were creating by generating the scaffold. So you either need to re-run the generation for the scaffold, or just manually edit the _form file to add the new columns.

Form partial isn't updated automatically after migration. You should manually add new fields to _form.html.erb

Try restarting your rails server and rails console. Worked for me.

Related

Changes in a model not saving. Rails 4.1

I've just created a user model using scaffolding, as follows:
rails g scaffold user name:string
I run db:migrate and everything works fine, but when I try to add a new field, it is not saving it in the database.
I add the field by doing:
rails g migration add_surname_to_users surname:string
No weird messages when I run rake db:migrate, the database table is ok, the form has been edited to alow surname input, as it have the show and index views to display it properly.
However, it is not saving the form value given into the database.
Any clues about how to fix it? It's quite annoying not being able to alter my models.
If you're using Rails4 (I think so), make sure you have this new attribute into user_params in your controller (because of strong_parameters):
def user_params
params.require(:user).permit(:name, :surname)
end
It's a typical mistake when new to this Rails version.
Further information here: strong_parameters

Delete model and migration file rails 4

I was attempting to add a tagging model to my rails blog. But I accidentally generated a "Tags.rb" model as opposed to "Tag.rb", after reading the guide I realized that making "tag" plural was a mistake when it comes to model. I rolled back the migration using
rake db:rollback
and then
rails destroy model Tags.rb
and this was what i got back
invoke active_record
remove /home/migration/templates/create_table_migration.rb
remove app/models/tags.rb.rb
invoke test_unit
remove test/models/tags.rb_test.rb
remove test/fixtures/tags.rbs.yml
When I come back to the model folder though its still there.
Please help :)
Just give
rails destroy model Tag
Try using tags plural without the additional .rb, like
rails destroy model Tags
Hope this helps
You can manually delete the files, or just manually remove the "s"s.
try this
bundle exec rake db:rollback
and then delete your model as
rails destroy model <model_name>
now totaly deleted.
You can delete the migration files in migrate folder and model in your model folder and then you can again create new one which you wants.
you have to run that commands
rails g model tag
Then migrate database
rake db:migrate
First off, when generating a Rails model using rails generate model, don't add .rb to the end of the model name. Rails automatically does that so if you add .rb yourself then you end up with a model named Tag.rb.rb, which is invalid.
You're running the remove generator with "Tags.rb". Did you generate the model originally using rails generate model Tags.rb? If you did, then running rails destroy model Tags.rb will hopefully remove all the files, but since the model name is invalid, it might not work as expected (your use case is outside the intended use case of the generator). If you originally generated the model using rails generate model Tags, then running rails destroy model Tags should work.
If you originally invoked rails generate model Tags.rb, and running the remove command isn't working (which again, in understandable given that Tags.rb is an invalid name--would have been nice if the generator had originally prohibited you from generating a Tags.rb.rb file, but you can't have everything):
If you can afford to destroy your database and rerun all the migrations, the easy solution will be to manually remove the added files, destroy the database, and rerun all migrations. Then you can add the correct model using rails generate model Tag.
If you can't afford to destroy your database, then you'll need to manually find the problem migration in the migration's folder, run the down command on that migration, and then manually delete that migration file and all the other files.

Tried to create a scaffold but migration already exists in Rails

I tried to create a new scaffold called Message and got this error message:
Another migration is already named create_messages
What can I do aside from changing the name of my model/controller/views (which I don't want to do)?
The reason this is occurring is I had a previous scaffold called Message that I changed to a different name (ran a migration to do this along with a search and replace for all filenames and variable/class names). I understand why I get this error message, just want to know how to move forward.
Quick fix is to rename old migration (create_messages) and try to generate scaffold again.
But recommendation is to have single create_messages migration so that it can be safely deleted by destroy scaffold command.
Update:
If already have Message model then no point in generating Message Scaffold, since scaffold does the same thing again with additional works like generating controller, routes etc.
If you already have a migration called create_messages you probably already have a Message model(Which means you can't have another named the same). If you have now decided you want to have a scaffold on the model to get the extra controller and view code use the following where name is the name of the column.
rails g scaffold Message name --skip

scaffold does not update view

I have a question to the following procedure:
script/generate scaffold product
title:string description:text
db:migrate
then I generate a migration which adds a column description to the
table products and migrate the db
again.
My question is: why is the field description not added to the project-views? Is that normal rails scaffold behaviour?
I think I saw in a video tutorial that the scaffold updates as well the views, which would be very convenient.
Thanks in advance for any help!
This is normal since scaffold does not "monitor" changes to the table or to any other scaffold-related resource (controller, model, views, tests, etc.).
then I generate a migration which
adds a column description to the table
products
You have description column already when generating the scaffold. Why do you need another migration for this?
This is normal behaviour for scaffolding, however there are alternatives.
If you were to look at Ryan Bates nifty_scaffold generator then this would allow you to re-run the scaffold generation. Assuming that you hadn't changed the generated code.
These generators can regenerate the views/spec/tests based upon the current state of the database model.
Be aware that if you have already customised the views yourself then they could be overwritten.

How do I add a field in Ruby on Rails?

I'm new to RoR, and I've just used scaffold to generate a table and create the pages for CRUD operations. Now I want to add a new field to this. One place I found tells me how to do that in the database, but is there a way to do it where it will add the field to all the pages too, or is that just a manual operation and I need to make sure I know all my fields up front?
To add a new column to the database
$ script/generate migration add_fieldname_to_tablename fieldname:string
$ rake db:migrate
To get your views up to date you can run the scaffold again, with your updated list of fields. It will balk on replacing your migrations but you can force it to replace your views.
$ script/generate scaffold tablename fieldname:string old_field_1:string ...
At the prompt answer a and it will overwrite the views, but not the old migration. It also won't modify your existing data.
First you'll write a migration to add the field, run the migration, then you need to rerun the scaffold to regenerate the views, etc. Beware, this will wipe out edited files from before. Of course, instead of scaffolding again you could manually add references to new field where appropriate.
You will have to update your database, no matter what (Remember to 'rake db:migrate' after you create the migration!)
Regarding the interface, you are a bit more lucky: the formtastic plugin makes your views look like this:
The 'f.inputs' is calculating the form fields on-the-fly, based on your model's attributes. This will not cover complex forms that need special treatment, but the usual ones, you will get them automatically.
For an easy-to-understand tutorial, see the latest railcast (Railscast #184, you will have to google for it, I can't post 2 links because I'm not cool enough for stackoverflow yet, sorry).
Railcast #185 is supposed to continue covering formtastic, and it's due to be published next monday.
Needs to be done manually, or the scaffold needs to be regenerated.

Resources