views:

49

answers:

2

Hi All,

I've encountered the following error while trying to create a blogging application. Any ideas why?


NoMethodError in Articles#show

Showing app/views/articles/show.html.erb where line #1 raised:

undefined method `title' for []:Array
Extracted source (around line #1):

1: <h2><%= @article.title %></h2>
2:  
3: <% if @article.category %>
4:   <p class="category">

From my limited understanding it's trying to tell me that there is no 'title' field in my 'article' database table, however as you can see from the desc below there is cleary a 'title' field!

mysql> desc articles;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| user_id      | int(11)      | YES  |     | NULL    |                |
| title        | varchar(255) | YES  |     | NULL    |                |
| synopsis     | text         | YES  |     | NULL    |                |
| body         | text         | YES  |     | NULL    |                |
| published    | tinyint(1)   | YES  |     | 0       |                |
| created_at   | datetime     | YES  |     | NULL    |                |
| updated_at   | datetime     | YES  |     | NULL    |                |
| published_at | datetime     | YES  |     | NULL    |                |
| category_id  | int(11)      | YES  |     | 1       |                |
+--------------+--------------+------+-----+---------+----------------+
10 rows in set (0.01 sec)

Help?!

Bernard

Ps. hope the formatting of the table above holds up... doesn't seem to look very good in the preview!


Controller code for relevant call as follows.

def show
if is_logged_in? && @logged_in_user.has_role?('Editor')
    @article = Article.find(params[:id])
else
    @article = Article.find_all_by_published(params[:id], true)
end
respond_to do |wants|
    wants.html
    wants.xml { render :xml => @article.to_xml }
end
end
+1  A: 

It looks like you're assigning an empty array to @article in your controller. It should instead be an instance of Article (if that's your model name). Can you paste your controller code?

nakajima
mrbernz
+3  A: 

I think you're trying to access the title element directly from the array. You'll need to iterate through each of the objects before you can access the properties.

for @article in @articles do |a|
   <h2><%= a.title %></h2>...
end

Just make sure to have in your controller...

@articles = Article.find(:all, :conditions => '...')

Update:

This part of your code returns a collection.

@article = Article.find_all_by_published(params[:id], true) 
treefrog
Hmm... dunno. In my controller I search for a particular ID in the Articles array, and populate @articles with that result? @article = Article.find(params[:id])Thought this would have meant that @article would be an element and not an array?
mrbernz
Yeah it does. I was only speculating.
treefrog