How to show post of a specific category - ruby-on-rails

I am trying to show only post from a particular category id
I have a category section and i have my relationship between categories and post working i just want to show post a specific category id instead of calling all post.
I have tried this but it's not working.
<% #posts.each do |post| %>
<h5><td><%=link_to post.category(1).title, post %></td></h5>
<% end %>

if you want to show a post of a category id, this should help
<% Post.where(category_id: 1).each do |post| %>
where that 1 would be the category id

In your code:
<% #posts.each do |post| %>
<h5><td><%=link_to post.category(1).title, post %></td></h5>
<% end %>
...this part looks suspicious: category(1). If post belongs_to category, try this:
post.category.title
If a post has_many (or has_and_belongs_to_many) categories, try this:
post.categories.first.title
One of those two should give you what you want.
Beyond that, I'm not sure you're trying to link. You're linking to a post, but the linktext is the category title. So, if you have three posts in the category, "Recipes," all three links will say "Recipes." Are you trying to link to the post or the category? Either way, the linktext should relate to the url.

Related

link_to blog path from blog_id in Post table

On my index page, I'm listing all the posts from all the blogs. How would I link_to from a post that has a blog_id to that actual blog.
I could easily in a controller do #blog = Blog.find(#posts.blog_id) if it were just one blog, but since it's not and I already have the blog_id for the post blog, I feel there has to be a way to do something like:
<% #posts.each do |f| %>
<%= f.title %>
#<%= link_to "Blog", go to the Blog using f.blog_id somehow? %>
<% end %>
The answer was given in a comment from "BroiStatse" and I thought I'd give the answer to it since he answered it in the comments section.
Basically had to do this simple line:
link_to 'Blog', blog_path(f.blog_id)

Rails link to a new post in a category without going to the category page

I have post model and a category model. And when i am in the show page of the model i can create a new post that belongs to the category with:
<%= link_to 'New post', new_page_path(:category_id => #category)%>.
However i would like you see the list of categories like this:
<% #categories.each do |category| %>
<h3><%= link_to category.title, category %></h3>
<% end %>
And i want by clicking on a category that you go directly to the new post form in that category. Instead of having to go to the category_show page first.
To direct the category link to new post form instead of category show page, you can just change the link as follows:
<% #categories.each do |category| %>
<h3><%= link_to category.title, new_page_path(:category_id => category) %></h3>
<% end %>
With this change, when you click on a category you would go directly to the new post form in that category.

Displaying a number on the current user's posts

This sounds like an easy question but for some reason I am lost.
In my user's profile page, I'm showing all of their posts. If they have 3 posts, I want to show something like this.
1. Post one title
2. Post two title
3. Post three title
So it shows the number to the left of the post. This cannot be the post's ID though. Do anyone know how to solve this?
Yes, check out Enumerable#each_with_index. So you can do something like:
#posts.each_with_index do |post, index|
puts "#{index} #{post}"
end
In this case you can rely on the HTML for the enumeration. Just use an ordered list.
<ol>
<% #posts.each do |post| %>
<li><%= post.title %></li>
<% end %>
</ol>
For anything more elaborate than this, Enumerable#each_with_index will be your best choice.

Displaying the last post item of each category

I have a ruby on rails application and i have two models posts and category. Category has_many :posts and Post belongs_to :category. Now how can i display the last post item of each category. i.e retrieving the last post in each category.
In your view something like:
<% #categories.each do |category| %>
<p><%= category.posts.last %></p>
<% end %>
the example above will order the records by id but you might want to order the result by "created_at" field (in case you have it):
<%= category.posts.order('created_at desc').last %>

Only show content when certain criteria is met?

I'm wondering if theres a best practice for what I'm trying to accomplish...
First we have the model categories, categories, has_many posts.
Now lets say, users add posts.
Now, I have a page, that I want to display only the current user's posts by category.
Lets say we have the following categories: A, B, and C
User 1, has posted in Categories A and B.
In my view I have something like:
#categories.each do |category|
category.name
#posts.each do |post|
if post.category_id==category.id
post content here
end
end
end
The problem with this, is I'm going to show the empty category, as well as the categories that do have content.
Is there a more efficient way of going about this? As I don't want to show the empty categories.
Best,
Elliot
UPDATE:
So I've been trying to use this code:
0}.each do |category| %>
For the most part its almost there. The issue is, it will still show an empty category if any posts have been entered in it at all (even if the current user has not input posts into that category.
So the question boils down to:
How do I make the following loop only count posts in the category from the current user?
0}.each do |category| %>
<% #categories.select {|cat| cat.posts.count > 0}.each do |category| %>
<%= category.name %><br/>
<% category.posts.select {|post| post.user == current_user}.each do |post| %>
<%= post.content %><br/>
<% end %>
<% end %>
This renders each category with any posts, then the content for each post within the category belonging to the current user. You'd probably want to do the initial selection in the controller though, to keep the view clean.
Adding to zetetic's answer, perhaps the lookup of the posts of a user in a given category would be done the opposite way. Instead of querying "All the posts for the category where the author is the current user", ask for "all the posts for the user where it is in given category"
<% #categories.select {|cat| cat.posts.count > 0}.each do |category| %>
<%= category.name %><br/>
<% current_user.posts_in_category(category).each do |post| %>
<%= post.content %><br/>
<% end %>
<% end %>
And throw a scoped search User#posts_in_category
EDIT: Probably you should also set the #categories variable already filtered from your controller, if you're not showing them somewhere else in your view. Also, if the category has no posts, it will not enter the cycle, so maybe the select is not needed.
#categories.select do |category|
category.posts.any? {|post| post.user == current_user }
end
Will filter out the categories to have only categories with posts by the current_user. I guess it would be more efficient doing this in the db, use this if you're fetching the #categories anyway.
As per my concern you need to have an intermediate table for users who publish posts for a given category that table might have following fields
user_id,post_id, category_id by this way you will be having a table which has category id's and for each category there is a post.
then you can do the following
get the distinct categories (from above table)
loop through the categories
get posts for those categories
** then you will not get any categories without posts
cheers,
sameera

Resources