Nested Route link_to loop route helper - ruby-on-rails

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.

Related

Link_to path in each do loop not working

In this Rails app, Users write Stories. A Story may be part of a Collection. Collections belong to the User who created it.
I am trying to show a single Story with links to the Collections it is part of. The collection.name part works but I can't get the collection_path right. Thanks for your help.
stories/show.html.erb
<% #story.collections.each do |collection| %>
<%= link_to collection.name, collection_path %>
<% end %>
rake routes for collections
user_collections GET /users/:user_id/collections(.:format) collections#index
POST /users/:user_id/collections(.:format) collections#create
new_user_collection GET /users/:user_id/collections/new(.:format) collections#new
edit_user_collection GET /users/:user_id/collections/:id/edit(.:format) collections#edit
user_collection GET /users/:user_id/collections/:id(.:format) collections#show
routes.rb
resources :users do
resources :collections
Solved it by using the following with the help of Sebastián Palma who answered this earlier.
<% #story.collections.each do |collection| %>
<%= link_to collection.name, user_collection_path(collection.user, collection), class: 'btn btn-lake' %>
<% end %>

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.

Seeking a better way to define my Rails routes/paths

I have 2 main components to my application, Users and Properties. The URL should be structured like: hostname.com/users/:user_id/properties/:property_id. I believe I've made a configuration error somewhere, because Rails never recognizes "property_path" or any of its variants, and I've had to hard code them in to get the redirects to work.
routes.rb
resources :users do
resources :properties
end
users/show.html.erb - Notice I had to hard code the path, instead of simply linking to "i"
<% #user.properties.each do |i| %>
<li><%= link_to "#{i.address}", "/users/#{#user.id}/properties/#{i.id}" %></li>
<% end %>
How can I better define my routes so that I can link above to just "i", which would represent the "properties_path", and would auto redirect to that show page?
You don't have to hardcode it. You can do:
<% #user.properties.each do |property| %>
<li><%= link_to property.address, [#user, property] %></li>
<% end %>
Yes, it's that simple. For more information, you can go to Rails guides.

Rails Linking to action in controller from view with data

i know there is a more elegant way to do this, but i can't figure it out, my brain must be stuck in the "S" gear.
<% #imageline.each do |album| %>
<%link_s = '/prepdownload?tag=gorilla'%>
<%=link_to (link_s) do %>
<%= image_tag src ='gorilla.jpg' %>
<%end%>
<%end%>
i have a controller action prepdownload that i need to pass the id of the image that get's clicked. As soon as i try to force the action, the other methods break down on me.
{"tag"=>"gorilla", "controller"=>"profiles", "action"=>"prepdownload"} i can't reverse engineer the result in a more elegant way than above.
Looks like you want a query string.
You could write out a manual URL hash like this:
<%= link_to {controller: "profiles", action: "prepdownload", tag: "gorilla"} do %>
<%= image_tag src ='gorilla.jpg' %>
<% end %>
But the elegant way is to use a named route
# config/routes.rb
get 'prepdownload', to: 'profiles#prepdownload', as: :prepdownload
then call the named route, passing in your query string parameters:
<%= link_to prepdownload_path(tag: "gorilla") do %>
<%= image_tag src ='gorilla.jpg' %>
<% end %>

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).

Resources