Displaying a number on the current user's posts - ruby-on-rails

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.

Related

How to show post of a specific category

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.

Combining Attributes for a Conditional

<% #user.chores.each do |chore| %>
<li><%= chore.name %></li>
<% end %>
<% #user.chorelists.each do |chorelist| %>
<li><%= chorelist.day %></li>
<% end %>
Hola, friends.
My app has three models: User, Chore, and Chorelist (which is a 'joined' resource for User and Chore).
Both of these blocks work, but how can I write, say, a conditional statement that will give me all of that user's chores for a specified day? (The days are saved in the Chorelist model as strings.)
If you need anymore more code from the app, just let me know. And thank you!
It'd be great to know what fields do your models have. Meanwhile, I think that you want something like this:
date = Date.today
#user.chores.joins(:chorelist)
.references(:chorelist)
.where(chorelists: {day: date})
And this should be in your controller, yeah, per commenter's suggestion.

Stuck trying to list associated model

I think I am deeply misunderstanding how to write instances.
Miniatures have_many Manufacturers through the Productions table.
On the miniatures show page I am trying to list all the manufacturers for the current miniature and have them link the Manufacturer show page. Like so:
<% #miniature.manufacturers.each do |manufacturer| %>
<%= link_to #miniature.manufacturer.name, manufacturer_path %>
<% end %>
Needless to say it does not work. It gives "undefined method `manufacturer'".
I have tried A LOT of different combinations to no avail. The following version puts all the manufacturers, rolled into one link, once for each manufacturer a miniature has, and links to /manufacturers. A big mess.
<% #miniature.manufacturers.each do |manufacturer| %>
<%= link_to #miniature.manufacturers.map(&:name).join(', '), manufacturer_path %>
<% end %>
I have been working on other things and hoping I would get the hang of this but I'm pretty sure it's something pretty fundamental about how I set up the instance.
If it's more likely something I need to add to the controller then I can add my controller code here. Any help much appreciated.
Does this work:
<% #miniature.manufacturers.each do |manufacturer| %>
<%= link_to manufacturer.name, manufacturer_path(manufacturer) %>
<% end %>

Can Ruby on Rails Automatically generate links to articles?

so my question is after i generate a scaffold and i have the ability to post articles to my site it usually is a link like www.mysite.com/articles/1
my question is...is it possible to have your application automatically generate a link to your www.mysite.com/articles/1 page
because right now i have to manually go into the HTML and add the link
=link_to 'my article', /articles/1
i was just wondering if its possible to make the application automatically generate the link for you?
Yes, it's possible. I don't know based on your question to what articles you would like to link, but if you have this set up using resources in your routes, this should work:
<ul>
<% Article.all do |article| %>
<li><%= link_to article.title, article %></li>
<% end %>
</ul>
Or if you're using HAML (as it appears you are based on the format of your question, but you don't specify:
%ul
- Article.all do |article|
%li= link_to article.title, article
You could modify the query to limit results, paginate them, sort them, etc. as you desire.

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