开发者

Accessing id from hash?

I am having trouble accessing the id from a hash:

@categories = Category.includes(:discussions)

The data is returned from the above.

<li class="category">
<h3><%=link_to(category.title, category.title)%></h3>
<%=link_to(category.discussions.first.title, CGI::escape(category.discussions.first.title), :class => 'category_discussion_latest')%>
Most recent by <%=link_to(category.discussions.first.comments.first.user.name, category.discussions.first.comments.first.user, :class => 'category_commentby_latest')%>
<span class="category_discussiondate_latest"><%= category_date(category.discussions.first.comments.first.created_at) %><开发者_C百科/span>
Discussions: <%=category.discussions.count%>
Comments: <%=category.comments.count%>
<div class="clear"></div>
</li>

This is my partial that renders the list.

However you may notice on the links at the moment I simply have category.title in the url section of link_to this is because if I state:

<h3><%=link_to(category.title, category.id)%></h3>

It returns:

undefined method `model_name' for Fixnum:Class

I am new to rails and i am sure this solution is simple but I just can't see it as I have been staring at the same code for too long. Thanks!


Please see the documentation for link_to. Passing an integer in as the second argument is not valid. You could pass a url (as a String) or an ActiveRecord model object, like this:

<h3><%=link_to category.title, category %></h3>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜