RoR - undefined method `title' for #<Product:0x596c148> - ruby-on-rails

i am new to RoR and following AWDwR book...
all was well till i started the Depot project....
created the products model using
ruby script/generate scaffold product title:string description:text image_url:string
now when i view the page http://localhost:3000/products it shows a generic page with nothing in the list and a link to add new product... when i click this link i get an error..the error is as follows...
undefined method `title' for #<Product:0x596c148>
then i checked the db..it seems there was no title column created at all... did a bit of searching for solutions and have got all the more confused...it appears to be a problem with scaffolding and version problems...
thanks in advance...

Perhaps you still need to run the migration? Try this:
rake db:migrate

Related

StatementInvalid : Could not find table while creating model object

I've just started programming using Instant rails and don't know much about it. So, I've made a rails project and created a database name contactlist_development and a table named contacts with some fields. I've generated a model named Contact and started the rails console using "ruby script/console".
But when i tried making a new Contact model object, it displays StatementInvalid : could not find the table 'contacts'.
Running rake db:migrate will execute the code you generated, hence creating the table.
Hope this helps!
Have you used rails generate model contact command to make Contact model? If yes, you forget to run command :
rake db:migrate
Or you can make table directly by creating migration or using sql query.

Wrong number of arguments(1 for 0) whilst using rails scaffolding

I just generated some scaffold code using the command rails generate scaffold Review Title Review:text rating:boolean --skip-stylesheets. That goes fine, but when I type in rails server to start the server, and navigate to /reviews I get an error message saying
ArgumentError at /reviews
wrong number of arguments(1 for 0)
I have not written any code myself, I have only used the scaffolding generator. How do I fix this?
Your name Review Title has a space in it. Try ReviewTitle.
UPDATE
Try:
rails generate scaffold Review title:string review:text rating:boolean --skip-stylesheets

Editing a resource in 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.

uninitialized constant CreateUsers::User

I am doing the following tutorial, to learn how to use dhtml in rails.
http://www.dhtmlx.com/blog/?p=426
I reach the point where I create the migration however when I do the rake db:migrate I recieve the following error:
An error has occurred, this and all later migrations canceled:
uninitialized constant CreateUsers::User
Don't seem to understand how I can possibly be recieving this error especially by simply following a tutorial. "/
Make sure you generate the User model before running the migration. (ruby script/generate model user in the tutorial, but rails g model User for Rails 3)
EDIT: As Jeremy pointed out, generating the model will create a migration file for you. The tutorial shows things in a somewhat backwards order.

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

Resources