6

While trying to generate a new scaffold using - collection> rails generate scaffold Items name:string subcategory_id:integer users_id:integer, i missed out the 'users_id:integer' and way i can go about having this attribute on it and not by running the full command again.

Thanks

El nino
  • 239
  • 5
  • 14

3 Answers3

4

Not really.

If you haven't added anything new to the existing scaffold yet (and are just worried about having to delete/recreate all those files) then you can run destroy first then recreate eg:

rails destroy scaffold Items
rails generate scaffold Items name:string subcategory_id:integer users_id:integer
Taryn East
  • 27,486
  • 9
  • 86
  • 108
4

If you haven't already pushed the migration, you could edit db/migrate/201112*create_items.rb and add the following line:

add_column :items, :users_id, :integer

If you have already pushed the migration, create another:

rails generate migration AddUsersIdToItems users_id:integer
rake db:migrate

Then you'll 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're running git, you could have run "git checkout ." after running the first scaffold generator, which would undo all your changes, then you could run it again.

Jason Noble
  • 3,756
  • 19
  • 21
  • Please what will be the effect of added
    :user_id, in the file
    – El nino Dec 05 '11 at 16:05
  • there seems to be a problem, my database table is not populated by the id of the user that created the item – El nino Dec 06 '11 at 01:26
  • The :user_id div is where you will select the User ID of the user you want the Item to belong to. If you want it to be populated with the ID of the current user, change the number_field to a hidden field and set its value to the ID of the current user. – Jason Noble Dec 07 '11 at 05:20
  • Thanks but after doing all, the table is not been populated even after adding information to it – El nino Dec 08 '11 at 01:01
0

This is a very old question but another answer just dawned on me after I created my own generator. If for some reason the attributes for a model changes, whether it be you added or removed the attribute through migrations and you want to reflect this in the scaffolded views, just run the scaffold with all of the attributes you want and also with the --skip-migration option. When prompted choose to overwrite the views that you want to overwrite.

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

This solution is for any new Rails users that have had this problem. It's not fun changing views by hand whenever the database changes.

CleoR
  • 806
  • 6
  • 18