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 %>
Related
I have the following in my projects_controller.rb:
def destroy
#project = Project.find_by_slug(params[:id])
#project.destroy
redirect_to projects_url
end
And I have the following in my routes.rb file:
delete "projects/:id", to: "projects#destroy", as: "destroy_project"
I have the following link (inside the show.html.erb file):
<%= link_to destroy_project_path(#project), method: :delete, class: "btn-gradient btn-red" do %>
<span>Delete Project</span>
<% end %>
Upon clicking the button, the page reloads. The show action is called upon clicking the button. I've added console logs in each method, and it is clear that the destroy action is never called.
Can anyone point me in the right direction?
the link_to helper receives 2 different sets of hashes for the options.
The first set is for things like the http method, and the second for the html attributes (class, id and so on)
The way you wrote it, you probably have method=delete in your query params, which is wrong. You have to explicitly enclose the method: :delete within its own options hash:
<%= link_to destroy_project_path(#project), { method: :delete }, class: "btn-gradient btn-red" do %>
<span>Delete Project</span>
<% end %>
If you use rails 7 with turbo framework you can try below for buttons.
<%= button_to "Delete this project", destroy_project_path(#project), method: :delete %>
Or you can try below for links.
<%= link_to destroy_project_path(#project.id) , data: { turbo_method: :delete } do %>
<span>Delete Project</span>
<% end %>
the problem that you are having is with the JS. there is a problem where something is not loading properly.
For testing try to remove your JS and use something like this
<%= javascript_include_tag "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.js" %>
When you get the delete method working, then you can debug where in your JS there is a problem.
at this time there is not sufficient info on your question to be able to know what it is.
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
I have a model, Notification, that has two fields: text and link. In my view for notifications, I have the following:
<% #notifications.each do |notification| %>
<li>
<%= notification.text %>
<%= link_to "View", notification.link %>
</li>
<% end %>
Examples of links include:
"foos/4/bars"
"about"
"foos"
However, when I attempt to follow the link, if I am in the "baz" controller, the result is an attempt at "baz/foos/4/bars", or "baz/about", rather than just "foos/4/bars" or "about".
Is there a better way to do this, or a way to disable the appending of the link to the current controller?
You trying to get a relative path to your current controller.
Try doing this ->
<%= link_to "View", "/" + notification.link %>
Thanks to #Kumar Abinash. The path was relative without a prepending "/". Simply changed links in the database to "/..."
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 %>
I have this tag: <%= link_to 'Show', user_listing_url(listing.user, listing) %> but instead of simply having it say 'Show' I actually want to place HTML inside of the <a> tag. Is this possible?
Example:
<div><div><img /></div></div>
yes you can pass a block to link_to
try something like this:
<%= link_to(user_listing_url(listing.user, listing)) do %>
<div><div><img/></div></div>
<% end %>
I totally recommend marflar's answer above.
However I would add one comment which is that if you are using html elements within a link_to block this may apply rails default link styling which may not be desirable.
One alternative is to use a button_to link but don't forget the default method for this is POST so specify the options as GET:
button_to(user_listing_url(listing.user, listing), method: :get) do %>
<div></div>
<% end %>