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.
Related
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 %>
I am trying to generate links in my nav-bar based on records.
I want to post a link in the patrols section of my app that correspond to a patrol route that an admin generated?
I have tried to place
#patrol_routes = PatrolRoute.all in the application controller
and then i want something like
<% #patrol_routes.each do |patrol_route| %>
<%= link_to patrol_route.name, patrol_route_path %> so that it takes me to the show page of the patrol route i want to access?
<% end %>
Is this possible? i have tried to google and research it, but I'm not finding anything, perhaps I'm not hitting the correct key words?
Assuming you have a route (as in config/routes.rb) named patrol_route you should do:
<% #patrol_routes.each do |patrol_route| %>
<%= link_to patrol_route.name, patrol_route_path(patrol_route) %>
<% end %>
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.
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.
I am trying to create a link to a record in my application:
<ul class="presentation-box">
<% #presentations.each do |presentation| %>
<li><%= link_to "Presentation", presentations_path(#presentation) %></li>
<li><%= presentation.author %></li>
<% end %>
</ul>
With the following line in the routes file:
resources :presentations
root :to => 'presentations#index'
For some reason, when I click the link it's taking me to the Presentation index view. I believe it should be taking me to the show view of the individual record?
Am I missing something obvious?
Your link_to is incorrect.
presentations_path will actually point you to the index, you want presentation_path(presentation) to point directly to the resource.
Also, you can just do <%= link_to 'Presentation', presentation %> and Rails will build the correct path for you
Change it to presentation_path(presentation)