Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 30th, 2007, 12:49 AM
fung fung is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Posts: 2 fung User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 9 sec
Reputation Power: 0
Talking Error while displaying data from two tables

Hi,

I am new to RoR and trying to develop a simple web application.

I have created two tables namely users and products.

class CreateProducts < ActiveRecord::Migration
def self.up
create_table "products" do |t|
t.column "id", :int
t.column "name", :string
t.column "stock", :int
t.column "price", :float
t.column "user_id", :int
end
end

def self.down
drop_table "products"
end
end
------------------------------------------------------------
class CreateUsers < ActiveRecord::Migration
def self.up
create_table "users" do |t|
t.column "id", :int
t.column "name", :string
t.column "balance", :float
end
end

def self.down
drop_table "users"
end
end
-------------------------------------------------------------

And the relationship between these two tables are shown below:

class Product < ActiveRecord::Base
has_many :users
end

class User < ActiveRecord::Base
belongs_to roduct
end
-----------------------------------------------------------------

However, when I tried to display data from the users table to a droplist, I encountered error as shown below:

NoMethodError in Product#index
Showing app/views/product/index.rhtml where line #25 raised:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.user_id

Extracted source (around line #25):

22: <br> <select name="product[user_id]">
23: <% @users.each do |user| %>
24: <option value="<%= user.id %>"
25: <%= ' selected' if user.id == @product.user_id %>>
26: <%= user.name %>
27: </option>
28: <% end %>

----------------------------------------------------------------

Can anyone tell me what's wrong with my code?

Thanks.

Regards,
Fung

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Error while displaying data from two tables

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap