0

I am new to ruby on rails, and I have a scaffold that was previously created.

The scaffold (let's call it "pet") had some original fields ("pet name", "pet type") I added a new column (let's call it "pet color")

I added it via "rails generate migration AddPetColorToPets pet_color:string:200" followed by "rake db:migrate", then "rails s"

Now, i thought that this would automatically update the "pet/new" and "pet/edit" pages by adding these variables. BUT it didn't.

So... I guess I'm asking - should it have? If not, then what do I need to do in order to transition my pages?

thanks

rockit
  • 3,708
  • 7
  • 26
  • 36

2 Answers2

2

No, it don't have to update the views created before, so, you MUST update the html, controller and maybe the model to work with the new field/attribute

rderoldan1
  • 3,517
  • 26
  • 33
  • I updated my views, the "article_params" - in my controller, and my model (for just verifying presence). The result is: NoMethodError in Petss#new --- on the line where I reference variable "pet_color"... – rockit Jun 04 '14 at 01:47
  • just to confirm, have you run rake db:migrate? – rderoldan1 Jun 04 '14 at 13:10
0

Modifying your model by creating or removing any fields in the database table or creating and running a new migration will not update the views or the controller for you. You will have to manually make the modifications yourself by adding in that new attribute into your views.

Now depending on your configuration, it can be as easy as:

rails destroy scaffold_controller ModelName

This will delete all the views and controller associated with ModelName.

In your case, you will run the command:

rails destroy scaffold_controller Pet

(This will delete all the views and the controller associated with the Pet model.)

As you can see, this will also get rid of any code you might have in those views and controller.

Please use this at your own risk.

Now to recreate the controller and the views with the new field, you run:

rails g scaffold_controller Model attribute1 attribute2 attribute3

This will generate all the views with the three supplied attributes, e.g.

rails g scaffold_controller Pet pet_name pet_type pet_color

This will recreate the views with the pet_color attribute in place.

Do remember to supply the attributes after the model name, otherwise, the views created will not reference any attributes. This is because when you use scaffold_controller, you tell Rails not to use a model, hence, you have to supply the attributes manually.

For more information with regards to rails generate and rails destroy commands, do a rails generate -h and rails destroy -h respectively.

For more information about Rails commandline, please take a look at the guide available on The Rails Command Line

If this approach is unacceptable (you already have written a lot of code in the views, etc.), you will have to manually go in and update the views accordingly.

You can also look into backing up the existing views and controller and once you have destroyed and regenerated the views and the controller as described above, you can copy paste the relevant code back again.