link_to blog path from blog_id in Post table - ruby-on-rails

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)

Related

Rails simplifies a post method

In online bootcamp project, I'm creating a reddit clone, and I'm at the stage where I'm developing support for accessing posts. Here's the method for displaying all of the posts in the index.
def index
#posts = Post.all
end
Here is the code that creates a link for each post, directing the user to the body of the post:
<% #posts.each do |post| %>
<p><%= link_to post.title, post_path(post.id) %></p>
<% end %>
Now the text states that "Rails lets us simplify this one step further, by allowing us to skip the post_path method altogether," and the resulting code in a separate file ends up omitting the post.id:
<% #posts.each do |post| %>
<p><%= link_to post.title, post %></p>
<% end %>
How is that possible? Does Rails just assume the post in question has the same id in the each element? Does making this change negatively affect the readability of the code?
No it doesn't assume that id is the same. Different object to each link_to, so different url/path for each link_to. This is just a simpler or to be precise a "pithier" way of using link_to, thats all. A bit of rails magic to help lazy developers.
link_to post.title, post is same as this
link_to post.title, post_path(post) or link_to post.title, post_path(post.id).
Rails has a concept of Polymorphic Routes which it uses to discern what the path should be, just simply using the object "post" instead of saying "post_path(post)" is enough in a link_to.
To understand this more, look at the implementation of link_to, url_for and polymorphic routes in Rails.

Nil id causing routing problems

I have 3 models: posts, comments and questions. Comments belong to posts and questions belong to comments. I'm trying to link from my posts show page to my questions show page. The posts show page is calling a partial _comments which the link is in. The problem is that the link goes to the questions index instead of the questions show because the question.id is nil. The URL looks like this:
/comments/19/questions/
The routes:
comment_question GET /comments/:comment_id/questions/:id(.:format) questions#show
comment_questions GET /comments/:comment_id/questions(.:format) questions#index
The link in the _comments partial:
<%= div_for(comment) do %>
<% comment.questions.select(:title).order('created_at desc').limit(3).each do |question| %>
<%= link_to question.title, comment_question_path(comment, question) %>
<% end %>
<% end %>
The partial is called in the posts show page with this:
<%= render :partial => #post.comments %>
I changed the link to this:
<%= url_for :controller => 'questions', :action => 'show', :comment_id => comment.id, :id => question.id %>
but got this error:
No route matches {:action=>"show", :controller=>"questions", :id=>nil, :comment_id=>20}
Thanks for the help!
Your question isn't nil, the question object just has no ID attached to it.
<% comment.questions.select(:title).order('created_at desc').limit(3).each do |question| %>
That line will build a query similar to the following (ignoring joins with comments):
SELECT title FROM questions
When you pass that to comment_question_path(comment, question) it just reads the attribute, it doesn't try to fetch it from the database. The ID in that question object will be nil because you haven't queried for it, hence why it's building a link with a nil question ID.
If you really want to use select, use comment.questions.select([:id, :title]) instead.
If you don't care about using select, just use comment.questions instead.
More info on select: http://guides.rubyonrails.org/active_record_querying.html#selecting-specific-fields

comment_question goes to comment_questions

I have three models: posts, comments and questions. I have a link from my posts show page that's supposed to go to the questions show view. Instead it goes to the questions index view.
Here are the rake routes:
comment_question GET /comments/:comment_id/questions/:id(.:format) questions#show
comment_questions GET /comments/:comment_id/questions(.:format) questions#index
and here is the posts view:
<%= div_for(comment) do %>
<% comment.questions.select(:title).order('created_at desc').limit(3).each do |question| %>
<%= link_to (question.title), comment_question_path(comment, #question) %>
<% end %>
<% end %>
Thanks for the help.
Try using question instead of #question in your each block:
<%= link_to question.title, comment_question_path(comment, question) %>
If the helpers give you trouble you can always override them with manual implementation
<%= #question.title %>

Displaying all comments for a ruby on rails blog

I'm another Rails newbie and have followed the Ruby tutorial on creating a blog.
Each post has many comments and the comments belong to the posts.
I can see the comments in the individual blogs and have created a show link to show the individual comment.
What I'd really like to do is create an index page for comments which shows all of them. I created an index action in the comments controller:
def index
#title = "All comments"
#comments = Comment.all
end
And an accompanying index page,
All comments
<% #comments.each do |comment| %>
Comment: <%= #comment.body %>
<% end %>
But I get an error:
undefined method `body' for nil:NilClass
My routes file:
resources :posts do
resources :comments
end
I'd really appreciate it if someone could point me in the right direction - I think my issue is that my comments are nested in the posts.
Thanks,
Bob
You should be using the comment passed into the block:
<% #comments.each do |comment| %> Comment: <%= comment.body %>
You're currently calling #comment.body, and #comment is nil because it is undefined in your controller and elsewhere.
So in your loop in the view file, you are iterating over the #comments array, creating a comment object for each of the comments in #comments. As such, try
<% #comments.each do |comment| %> Comment: <%= comment.body %>

Adding "Featured Posts" to my blog

I am trying to add a featured post feature to my Ruby on Rails Blog. So far I have added a featured_post column to my post table and it passes a 1 if the check box is selected and 0 if not.
Now I am attempting to pull out these posts by doing the following:
/views/posts/index.html.erb
<% #featured_post.each do |post| %>
<%= post.title %>
<% end %>
And in the posts_controller.rb I am doing the following in the index action:
#featured_post = Post.all
Obviously this brings in all the post titles which is not what I want. I am assuming I have to add something to the controller to all for this but not sure what that is.
In your post model, write this
named_scope :featured,:conditions => {:featured_post => true }
write this in your controller
#featured_posts = Post.featured
and in view use this,
<% #featured_posts.each do |post| %>
<%= post.title %>
<% end %>
now you should get all the featured posts.

Resources