Editing a resource in Ruby on Rails - ruby-on-rails

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.
rails g migration add_image_to_posts image:string
Which will generate a file in db/migrate
You can then run this migration with:
rake db:migrate
And you should now have an image field in your model.

Related

Tried to create a scaffold but migration already exists in Rails

I tried to create a new scaffold called Message and got this error message:
Another migration is already named create_messages
What can I do aside from changing the name of my model/controller/views (which I don't want to do)?
The reason this is occurring is I had a previous scaffold called Message that I changed to a different name (ran a migration to do this along with a search and replace for all filenames and variable/class names). I understand why I get this error message, just want to know how to move forward.
Quick fix is to rename old migration (create_messages) and try to generate scaffold again.
But recommendation is to have single create_messages migration so that it can be safely deleted by destroy scaffold command.
Update:
If already have Message model then no point in generating Message Scaffold, since scaffold does the same thing again with additional works like generating controller, routes etc.
If you already have a migration called create_messages you probably already have a Message model(Which means you can't have another named the same). If you have now decided you want to have a scaffold on the model to get the extra controller and view code use the following where name is the name of the column.
rails g scaffold Message name --skip

Adding a Reference Column to an Existing Table During Association in Rails3.2.9

I have two models in my rails application. One is User that I have generated through with Devise and another one is Links. Now Initially There was only the Link model in my application. So I had two columns in my model Link namely "id" and "link" . Now my requirements have changed and I need implement a User model. A user in my application has one or many links. So added the line has_many :links to my "user.rb" and belongs_to :users to my "link.rb" file.
And I generated a migration like:
rails generate migration add_user_id_to_link user:references
That is, I'm generating a migration file to modify my existing table "links" and add a reference column "user_id" to the table.
But it's giving an error when running rake db:migrate. Then I ran the command:
rails genearate migration add_user_id_to_links user_id:integer
But it's giving a command not found error. Now the question is how do I achieve this??
Thanks in advance...
I'm pretty sure the references helper only works for create_table. Which is why the first command doesn't work.
The second command should have worked though. The error is perhaps pointing to your typo in generate.
I hope that helps.
You can do rails g migration add_user_id_to_links user_id:integer:index if you want to assign each user to a link that they have posted.

render 'form' not showing columns added using a migration

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.

RoR NameError - Ruby on Rails MySQL

NameError in GenresController#index
uninitialized constant GenresController
RAILS_ROOT: C:/Users/Will/Desktop/INSTAN~1/rails_apps/talewiki
I've created a table called Genres and when I try to connect to it via local host I get the above error.
Any ideas?
With all the questions you're asking I believe you're an absolute beginner regarding ROR. Perhaps you should visit some tutorials to learn rails.
I don't know what your genre model describes, but I think it will have a name.
Basic steps for a basic genre model:
Delete the table for your genres if created manually (with SQL code)
DROP TABLE genres;
generate a complete scaffolding for genres:
$ ruby script/generate genre name:string
$ rake db:migrate
Now you have a complete controller for all CRUD actions for a simple genre model
If I were you I would read some tutorial about RoR, because you make the impression that you don't understand RoR or the MVC principle behind it. A good start would be: http://storecrowd.com/blog/top-50-ruby-on-rails-tutorials/
You need to generate a controller to handle the index action when your browse your application on localhost
ruby script/generate controller genres index
run that from your console within your application and it will generate the GenresController with the action index (it will be an empty action but you shouldn't see an error when browsing http://localhost:3000/genres/)
file
C:/Users/Will/Desktop/INSTAN~1/rails_apps/talewiki/app/controllers/genres_controller.rb
must be present

How do I add a field in Ruby on Rails?

I'm new to RoR, and I've just used scaffold to generate a table and create the pages for CRUD operations. Now I want to add a new field to this. One place I found tells me how to do that in the database, but is there a way to do it where it will add the field to all the pages too, or is that just a manual operation and I need to make sure I know all my fields up front?
To add a new column to the database
$ script/generate migration add_fieldname_to_tablename fieldname:string
$ rake db:migrate
To get your views up to date you can run the scaffold again, with your updated list of fields. It will balk on replacing your migrations but you can force it to replace your views.
$ script/generate scaffold tablename fieldname:string old_field_1:string ...
At the prompt answer a and it will overwrite the views, but not the old migration. It also won't modify your existing data.
First you'll write a migration to add the field, run the migration, then you need to rerun the scaffold to regenerate the views, etc. Beware, this will wipe out edited files from before. Of course, instead of scaffolding again you could manually add references to new field where appropriate.
You will have to update your database, no matter what (Remember to 'rake db:migrate' after you create the migration!)
Regarding the interface, you are a bit more lucky: the formtastic plugin makes your views look like this:
The 'f.inputs' is calculating the form fields on-the-fly, based on your model's attributes. This will not cover complex forms that need special treatment, but the usual ones, you will get them automatically.
For an easy-to-understand tutorial, see the latest railcast (Railscast #184, you will have to google for it, I can't post 2 links because I'm not cool enough for stackoverflow yet, sorry).
Railcast #185 is supposed to continue covering formtastic, and it's due to be published next monday.
Needs to be done manually, or the scaffold needs to be regenerated.

Resources