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
Related
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.
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.
I am pretty new to Ruby on Rails, and I was wondering if there was a way to edit the database schema for a model.
For example, I have the Subscriber model in my application -- the way I created it was by using rails generate scaffold Subscriber email:string
But now, I want a name in the subscriber model as well. Is there any easy way to do this? I have put a lot of code in my current controllers and views, so I don't necessarily want to destroy the scaffold, but I would like to edit the model.
Thanks in advance,
hwrd
P.S. I am using Ruby on Rails 3
An ActiveRecord Model inspects the table it represents. You don't actually need to change your model just to add a new field (unless you want to add validations, etc).
What you want to do is make a new migration and then migrate your database up:
rails g migration AddNameToSubscribers name:string
rake db:migrate
Then you can start referencing the name field in your controllers and views.
(This generator command might seem a little magical, but the rails generator recognizes this format and will generate the appropriate add_column and remove_column code. See the Rails migration guide for further reading.)
If you mean changing the database schema of your model, you'll want to use migrations.
You'll do things like
add_column :city, :string
remove_column :boo
http://guides.rubyonrails.org/migrations.html
If you do only mean finding models and updating the data inside each instance, go with #apneadiving's answer.
I am a Rails beginner and trying to understand how rails migration works.
I have created a scaffold like:
script/generate scaffold Item col1:string col2:text
rake db:migrate
I would like to add another col4 using migration:
I created a migration as follows:
class AddCol4 < ActiveRecord::Migration
def self.up
add_column :items, :col4, :numeric
Item.reset_column_information
end
def self.down
remove_column :items, :col4
end
end
When I run rake db:migrate the new column gets added. However the view is out of sync.
Am I supposed to manually add the new column to the view? Is there a way to auto-regenerate the model/view using the new table columns?
Sorry, it is a basic question but from my experience with other frameworks, it should have been automatic.
The rails guide on migration does not make this obvious regarding how the synchronization is supposed to work after you perform a migration.
Unfortunately you will need to modify the view manually. The view is created by running the script/generate scaffold command. Migrations only change the database. Technically, you can rerun the scaffold command and have it regenerate the view. It will ask you if you want to overwrite the previous file, however, if you go this route, you will still need to specify ALL of the columns that you want. You can't simply add some here and there.
If you are early in development, then you might take this route. Simply run
script/destroy scaffold Item
and then rerun
script generate scaffold Item col1:string col2 string col3:numeric
There are some dynamic scaffolding extensions available such as ActiveScaffold if you are creating something that only a few users will see, but I would recommend doing the HTML yourself as it will always come out the way you want.
I can't seem to find any of the other dynamic scaffolding plugins. There used to be quite a few...
I have a database with tables. I want to create a model in my Rails app from existing table. As i know, such functionality is available, and is done as follows:
script/generate scaffold model_name --skip-migration
Of course, i defined my database in database.yml file. Scaffold generated for me a model with controller and views. My table name is not as it must be for Rails(it is incorrect, not following conventions), i added set_table_name to my controller. But, when i am calling the index method, on my page i have only set of # symbols, but not a data from database. In my index.html.erb i have only generated code by scaffold. How can i print out my database data?
Have you generated a schema file from your existing database? If you run the command
rake db:schema:dump
and then re-generate your scaffold this should fix the problem.
Additionally you may wish to check out Dr Nic's Magic Model generator. This will generate models for all of your existing tables and attempt to guess the relationships. This will probably not work if your table naming is not understandable by rails.
UPDATE
I do not generally use the default scaffold however I have tested this myself and it appears that if you skip the migration and do not pass any column name/type pairs then the scaffold generator will not create anything in the template to render the columns.
You have two choices here either
Pass in the column name pairs as well as skip-migration or
Download Ryan Bates Nifty Scaffold generator which will create the scaffold with the column names even if you specify --skip-migration