I'm trying to create a link that takes you to a specific users page in my rails app. The link currently looks like this:
<%= link_to track.user.username, user_path(#user) %>
However, I get this error when I try to refresh:
No route matches {:action=>"show", :controller=>"users", :id=>nil}, missing required keys: [:id]
Can somebody suggest what I could be missing? I'm not sure how to specifically reference the users id in the code.
Thanks,
Ant
Right apologies! I've figured it out, the link_to is placed inside of this slightly hidden loop in my code:
<% #tracks.each do |track| %>
// lots more code in here
<%= link_to track.user.username, user_path(#user) %>
<% end %>
so what i had to do was reference track first and then find the user like so:
track_path(track.user)
so my code now looks like so:
<% #tracks.each do |track| %>
<%= link_to track.user.username, user_path(track.user) %>
<% end %>
and this works perfectly fine!
Thanks all :)
Ant
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 %>
Pretty new to rails. I'm doing a project where a user submits a url and a title. The title is supposed to link to the url provided by the user. The url is stored as a param for link.
Here's the code from the index view:
<% #links.each do |link| %>
<%= link_to link.title, link.url %>
<%= link_to "comments", link %>
<% end %>
This works, for the most part.
The problem occurs if the submitted url doesn't begin with http://. As it is, it's pointing to http://localhost:3000/google.com and I get an error No route matches [GET] "/google.com"
How could I get around this? I tried changing it to:
<%= link_to link.title, "http://#{link.url}" %>
Which makes google.com work, but then http://google.com turns into http://http//google.com somehow.
I'm sure the fix will be a face palm moment!
Prepend url with protocol if it's absent:
module ApplicationHelper
def url_with_protocol(url)
/^http/i.match(url) ? url : "http://#{url}"
end
end
<%= link_to link.title, url_with_protocol(link.url) %>
answer derived from this SO question/answer
In your input field, you can do something like <input type="text" name="url" value="http://"> so that your url will always started with http://. User can also manually changed it to https if needed.
Also I may add a full_url method to the model that adds it if it's missing.
I am trying to use link_to in rails 4 with controller and html options and a do..end block. I have seen similar posts but have not been able to use any of the answers successfully.
Working code without a do..end block:
<%= link_to 'recommend', { controller: 'recommendations', id: offer.id }, method: :post %>
When I try to use some embedded ruby to add extra information to the link, I cannot get it to work:
<%= link_to( { controller: 'recommendations', id: offer.id }, method: :post) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
The code compiles but in the rendered, the link that is generated is the following:
<a controller="recommendations" id="38">
<p>Some Html</p>0
</a>
Any help would be appreciated. I think that it is a small problem with the syntax but I have tried all manner of brackets, spaces etc that I could think of without luck.
UPDATE: I have tried the following code without success:
<%= link_to( { controller: 'recommendations', action: 'create', id: offer.id }, method: :post) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
The HTML output is:
<a action="create" controller="recommendations" id="39">
<p>Some Html</p>0
</a>
This might not be important but as a side note, the create action doesn't have a helper function for links. When I run the
rake routes
command I get the following
...
recommendations GET /recommendations(.:format) recommendations#index
POST /recommendations(.:format) recommendations#create
new_recommendation GET /recommendations/new(.:format) recommendations#new
...
In my opinion this isn't a problem but it is a reason why code such as:
link_to create_recommendation_path
won't work. Finally, the intention of the link is to act as a 'like' button. It creates a recommendation and then displays the current page again. Once again, thanks for the help in advance.
The reason link_to create_recommendation_path doesn't work is because there is no named route for create_recommendation_path, only for recommendations_path. You can see the named routes in your routes list (which you have in your post above). The left most column that comes out of routes shows the named routes. Notice that recommendations#create doesn't have an entry in the list.
You could probably get the path you want with
<%= link_to recommendations_path(:offer_id => offer.id), :method => :post do %>
html stuff
<% end %>
This should post to a path that looks like
/recommendations?offer_id=<the offer id>
(except the post data will be in the headers not on the URL)
This will work if the create method going to do something like
Recommendation.create(params)
and the only parameter you need to create a new Recommendation is an offer_id
What I don't understand is why you're trying to POST with a link? Does creating a recommendation only require an offer id?
In your link_to you're only specifying a controller, you need to also specify the action otherwise it doesn't know where to route it to. Either use:
<%= link_to({ controller: 'recommendations', action: 'show', id: offer.id }) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
Or
<%= link_to({ show_recommendations_path(id: offer.id) }) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
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).
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.