Rails simplifies a post method - ruby-on-rails

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.

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)

How to turn blog title into a link on the index page in rails 4 using Friendly ID gem

I'm still very much a novice and can't figure out how to get my blog titles to link to the full post. I'm using the friendly id gem and built this basic blog following the Getting started instructions at http://guides.rubyonrails.org/getting_started.html
Below is my current code which converts the title into a link but all titles are continuing to link to the index page for posts.The linkable title should point to something similar to this example:
example.com/posts/my-first-blog-post
<h1>The Blog!</h1>
<div class="container">
<div class="row">
<div class="col-md-1">
<% #posts.each do |post| %>
<h2><%= link_to post.title %></h2>
<P><%= truncate (post.body), :length => 250 %></p>
<% end %>
</div>
</div>
</div>
I would greatly appreciate any help getting these title to link to the link name created using friendly id. If it makes any difference the slug is generated from the title and I did add and save the slug for all previously published blog post when I wasn't using friendly id.
Thank you in advance for the help.
The method signature (the way you use it) is:
link_to text_to_display, path
If you follow Rails standards, you should do:
link_to post.title, post_path(post.slug)
Actually .slug is not required because FriendlyId overrides the to_param method in your model.
So you can do:
link_to post.title, post_path(post)
Or even
link_to post.title, post
Up to you...
In your model, you should have something like
friendly_id :title, use: :slugged
Your link should simply be
link_to post.title, post
You don't have to introduce the "slug" in your views. Simply updating the model will do!
One way we like to use is to not include the attribute of the element:
<%= link_to post.title, post_path(post) %>
If you have friendly_id set up in your model, this should work automatically

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.

Creating Links on Rails

I've just started using rails yesterday, so this is a kinda noob question
for example, a user is at www.example.com/name
and I want to make several links to www.example.com/name/:id
So I tried something like this:
<% #items.each do |item| %>
<%= link_to item.name, '/name' :id %>
<% end %>
I know, it was a complete guess on how I should write the code, but the restful code sends to a completely wrong link. How should I write this three lines?
Use the route helper:
<% #items.each do |item| %>
<%= link_to item.name, item_path(item) %>
<% end %>
ps: when you have a simple question like this one, take a look at this guide, you'll often find the answer.
Try
<%= link_to item.name, item_path(item) %>
item_path is a URL helper method which spits out the link to show a name.
URL helpers have the general form:
{action}_{class}_path({object or object_id})
If {action}_ is omitted, then the default action is assumed (normally show).

Nested Route link_to loop route helper

If I visit this page /articles/1/comments
Why won't this work (views/comments/index.html.erb)
<% #comments.each do |comment| %>
<%= link_to "show", article_comment_path(comment)
<% end %>
and this will?
<% #comments.each do |comment| %>
<%= link_to "show", article_comment_path(#article, comment)
<% end %>
routes.rb
resources :articles
resources :comments
end
I would think the route helper would be smart enough to infer I want to use the article in the current context...
Magic is pretty nice except when you spend a lot of time expecting it to be magical and it's not :P
You cannot expect too much. This way you still have the freedom to use an instance variable, a plain parameter. The link_to helper can also be used outside the context of the controller. Furthermore, the list of possible parameters is dynamic. If you give one parameter, it has no way of knowing which you did specify: the article? The comment?
Note that you can just write:
link_to "show article", #article
link_to "show comment", [#article, comment]
Hope this helps.

Resources