0

Ok, I searched all over the web and found no answer. I am looking for a way to display a name of a 'category' in the show view of a post (I have to mention I'm rookie in Rails).

I have....

a model "Post"

class Post < ActiveRecord::Base
    has_many :categories
    belongs_to :user
end

a model "Category"

class Category < ActiveRecord::Base
    belongs_to :post
end

and the "show" view of the "post" has a line like this

<%= @post.category.name %>

The error message as screen shot: NoMethodError in Posts#Show - undefined method `category' for #

The "show" action in "Posts" controller:

def show
    @post = Post.find(params[:id])
end

I'm building this app along a little outdated training video on udemy. In this video there's in the category model a line with "attr_accessible"

class Category < ActiveRecord::Base

    attr_accessible :name           <---------------------- this
    has_many :posts
end

...but this does no longer exists since Rails 4.0. Is there another way to retrieve the category name of the post?

Thank you in advance :-)

dvdt
  • 39
  • 7
  • After thinking a while... was was not clear mentioned is that I want to have the category of the post it self to be shown. E.g. there are four categories 'cars', 'cycles', 'boats' and 'planes' and the actual post in the show view is a text about boats, than I want there to be displayed "boats" as category name under the text. Not all available categories (made with a block) and not the first category (made with "...categories.first"). – dvdt Feb 12 '16 at 07:28

3 Answers3

1

The method category not exists because the Post model has many "categories" not one "category". The Post should have the method "categories". Then if you want to show a first "category" of post in the view:

<%= @post.categories.first.name %>

If you want to show all "categories" of post, then you iterate the collection:

<% @post.categories.each do |category| %>
<%= category.name %>
<% end %>
Armando
  • 940
  • 7
  • 16
  • Hi Armando, thanks for very fast reply. I've tried it sometime inbetween but than there's is only "category" to read but not the name of the category. I've look on. But it was anyway good to get this point out what has to be written is this code line. Thanks. – dvdt Feb 12 '16 at 06:28
1

I got the answer. I found out, that every way, to get the data out of the table categories in the view for products won't work. I than thougt, showing the category in the category show view is simple working. With this idea in mind I took the same code, this one:

app/views/categories/show.html.erb

<p>
  <strong>Name:</strong>
  <%= @category.category_name %>          <--------this line
</p>

...from the category show view and put it into the post show view. I than got again an error but different:

--> NoMethodError in Posts#show

Ok, this says the instance variable "@category" isn't available for the post show view. To change that was easy. I copied the object from the categories controller's show action into the posts controller's the show action. Like this:

class PostsController < ApllicationController
 .
 .
 .
  def show
    @post = Post.find(params[:id])
    @category = Category.find(params[:id])
  end

And: it works!!!! But now, is there something wrong to do it like this?

Cheers.

dvdt
  • 39
  • 7
  • Found out another fix to that problem. It was because the foreign key was not set up by rails. I had to add it manually to the table. Than the first shown way worked well. Good learning for the next app. I'll post the setup latest next week. – dvdt Feb 13 '16 at 13:53
0

I tried again and found the real error in my code. the foreign key was not set by Rails. I had to do manually. The table which has a "belongs_to" (in my case the posts table) needs a foreign key added (like category_id).

First creating the migration file:

rails g migration add_foreign_key_to_posts_table

Second adding the migration code to the migration file:

class AddForeignKeyToPostsTable < ActiveRecord::Migration
  def change
    add_foreign_key :posts, :categories
  end
end

Third raking the db migration with:

rake db:migrate

Fourth adding the foreign key for other resources by following steps one to three. Now everything works fine.

dvdt
  • 39
  • 7