Adding an additional attribute to the rails command

When trying to create a new scaffold using - collection> rails generate scaffold Items name:string subcategory_id:integer users_id:integer I skipped "user_id: integer" and I can use this attribute rather than repeating the full command.

thanks

+6
source share
3 answers

If you have not clicked on the transfer yet, you can edit db / migrate / 201112 * create_items.rb and add the following line:

 add_column :items, :users_id, :integer 

If you have already moved the migration, create another:

 rails generate migration AddUsersIdToItems users_id:integer rake db:migrate 

Then you have to manually edit the scaffold code:

 vi app/views/items/_form.html.erb 

Copy the <div> for subcategory_id and change it to: users_id

 <div class="field"> <%= f.label :subcategory_id %><br /> <%= f.number_field :subcategory_id %> </div> <div class="field"> <%= f.label :users_id %><br /> <%= f.number_field :users_id %> </div> 

Alternatively, if you use git, you can run "git checkout". after starting the first scaffold generator, which cancels all your changes, you can start it again.

+4
source

Not really.

If you have not added anything new to the existing scaffold (and just worry about the need to delete / recreate all these files), you can start the destroy first and then recreate, for example:

 rails destroy scaffold Items rails generate scaffold Items name:string subcategory_id:integer users_id:integer 
+4
source

This is a very old question, but another answer that I heard after I created my own generator. If for some reason the attributes for the model change, whether you added or removed an attribute through migrations, and you want to reflect this in visual views, just run the scaffold with all the necessary attributes, as well as using - skip-migration. When prompted, select to overwrite the views you want to overwrite.

If you forgot to add an attribute, use the transition to add it (rails g migration AddXXXToYYY field: type), and then run the scaffold again, as I explained above.

This solution is for any new Rails users who have had this problem. It is not interesting to change the views manually each time the database is changed.

0
source

Source: https://habr.com/ru/post/903102/


All Articles