To begin with, I must say that I am VERY new to Ruby on Rails. I’ve only been working my way through it for about a week.
I’ve starting integrating my existing website onto a blog I made which uses two resources – posts and comments.
My problem resides with the posts resource. I made the posts resource using “generate scaffold”, to give me the fields “Title” and “Content”, however I would now like to add another field “Image”, which takes a local url of an image to add to the blog.
I’ve gone through manually and added references to the new “Image” field, adding it as a string with the intention of using the string as a target in an image_tag when it need to be displayed.
However when I visit pages that use the form to add or edit posts, I’m given the following error:
NoMethodError in Posts#new
undefined method `image' for #<Post:0x39a4868>
Is there an automated, catch-all way of adding a new field to a pre-existing resource?
Any help would be appreciated.
You need to generate a migration.
Which will generate a file in db/migrate
You can then run this migration with:
And you should now have an image field in your model.