943,793 Members | Top Members by Rank

Ad:
  • Ruby Discussion Thread
  • Marked Solved
  • Views: 3445
  • Ruby RSS
Dec 31st, 2007
0

Simple Rails Application trouble

Expand Post »
Hey everyone,

I am new to Ruby on Rails and I am having a problem that I can't seem to figure out. I have a mysql database that I created and I have created a model for it. I have did not create a migration, as the database is already full of information. I added a scaffold to the application, and it showed the data from the database with no problems.

I have tried to create a new controller called portfolio, and in this I have an index. Here is the controller code:

Ruby Syntax (Toggle Plain Text)
  1. class PortfolioController < ApplicationController
  2. def index
  3. @stocks = Stock.find_active_symbols
  4. end
  5. end

The @stocks is, from what I understand, supposed to contain the rows returned from the database. Below is my Stock Model code:

Ruby Syntax (Toggle Plain Text)
  1. class Stock < ActiveRecord::Base
  2.  
  3. def self.find_active_symbols
  4. find(:all, :order => "stock_symbol")
  5. end
  6. end

Finally, I have a view created in app/views/portfolio/index.rhtml Below is the code from there:

Ruby Syntax (Toggle Plain Text)
  1. <% for st in @stocks -%>
  2. <%= h(st.stock_symbol) %>
  3. <% end %>

in the database table, I have a column named stock_symbol. When I try and access this view in a web browser, I get the following error message:

Ruby Syntax (Toggle Plain Text)
  1. undefined method `stock_symbol' for #<Stock:0xb72e11a4>
  2.  

I don't understand why I am seeing this. Anyone have any ideas?

Thanks for the help in advance.

Nick
Reputation Points: 18
Solved Threads: 4
Junior Poster
stupidenator is offline Offline
192 posts
since Mar 2005
Jan 1st, 2008
0

Re: Simple Rails Application trouble

Looking at your code I can't see whats wrong.

What happens if in your view you do

Ruby Syntax (Toggle Plain Text)
  1. <% for st in @stocks -%>
  2. <%= h(st.inspect) %><br />
  3. <% end %>

It may give a clue as to what's wrong.
pty
Reputation Points: 64
Solved Threads: 39
Posting Pro
pty is offline Offline
530 posts
since Oct 2005
Jan 1st, 2008
0

Re: Simple Rails Application trouble

Since you're working with a MySQL database that's already full of data, do they follow Rails conventions? I.e., are the table names plural (e.g., the table corresponding to the Stock model should be named "stocks") and does each table with a Rails model class have an "id" field that's its primary key?

If not, that would cause problems (though I'm not sure if it would cause the problem you're seeing at the moment).

Ed
Reputation Points: 10
Solved Threads: 1
Newbie Poster
eruder is offline Offline
3 posts
since Jan 2005
Jan 2nd, 2008
0

Re: Simple Rails Application trouble

Eruder,

I believe that is the problem. I added a column called id and set it as the primary key and now it seems to work fine. Thank you for your help.
Reputation Points: 18
Solved Threads: 4
Junior Poster
stupidenator is offline Offline
192 posts
since Mar 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Ruby Forum Timeline: My first Ruby programming question :-)
Next Thread in Ruby Forum Timeline: Amazon-Ecs Local Download Slow on Leopard





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC