Show name instead of ID in has_many view table

雨燕双飞 提交于 2020-01-01 06:30:23

问题


I have tried a couple other similar posts but am still getting an error.

In the Posts model I have a category_id field. I have the following models:

#Posts model
belongs_to :categories  

#Category model
has_many :posts

In the Posts index controller I have:

@categories = @posts.Category.find(:all, :order => 'categoryname')

In the View I have:

<% @posts.each do |post| %>
<tr>
<td><%= post.category_id %></td>
<td><%= @categories.categoryname %></td>

<td><%= link_to 'View', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
</tr>
<% end %>

In the 2nd column I am trying to show the category name ("categoryname") from the Category table instead of the category_id from the posts table. I am getting an error:

undefined method `Category' for #ActiveRecord::Relation:0x3e1a9b0>

I have also tried:

<td><%= post.categories.categoryname %></td>

But get the same error.

As well as:

<td><%= post.category.categoryname %></td>

Any suggestions would be greatly appreciated.


回答1:


In your model

belongs_to :category

In your view

<td><%= post.category.categoryname %></td>

You can get rid of the @categories = line in your controller

Also, categoryname is probably not the best attribute name for your Category model. Why not just name it name. post.category.name seems a lot better than post.category.categoryname, don't you think?




回答2:


Okay, a couple things

belongs_to :categories  

belongs_to is a singular relationship. You should be putting

belongs_to :category 

In this case you need category_id in the posts table. You would get the category by

@post.category.categoryname

Unless a post can have many categories, in which case you'd want

#Post
has_and_belongs_to_many :categories

#Category
has_and_belongs_to_many :posts

In this case you need a join table called categories_posts with two fields category_id and post_id and you would get it by calling

@post.categories.each do |cat|
  cat.categoryname
end

There are some other problems with you code, like

@categories = @posts.Category.find(:all, :order => 'categoryname')

Category is a model, not your named relationship, which is probably why you are getting the exception in your application.




回答3:


You will get the following error

undefined method `categoryname' for nil:NilClass on the line: <%= post.category.categoryname %>

if there are records in your table without a category specified.

In other words, make sure all of your records have a category associated with them




回答4:


i Noticed that @category in your controller starts with a small letter c, but in your view, it starts with a Capital letter !




回答5:


In your case, if there is a chance that you can have an empty category_id in posts table (as you mentioned in your comment), you can add

if post.category_id?

so your cell will look like this:

<td><%= post.category.categoryname if post.category_id? %></td>

If post.category_id is null it will just show an empty cell.



来源:https://stackoverflow.com/questions/10762669/show-name-instead-of-id-in-has-many-view-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!