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 %>
Related
I'm learning RoR building social network. So in my views I have a index view which is rendering a mix of all the posts from my groups.
So now, I would like to build a link to redirect toward my original post (into his group). Redirect into the group is not really a problem, but I don't know how to redirect to my post into this group.
With a code example it will be more clear :
Index view :
<%= #posts.each do |p| %>
<%= p.title %>
Publish in <%= link_to p.group.name, group_path(p.group, ROUTE TO MY POST) %>
<% end %>
Show view(group):
<%= #group.posts.each do |post|%>
<div id='post_iter<%=post.id%>'
<%= p.title%>
</div>
<% end %>
So I would like to redirect the user toward his iteration into the group Show.
Something like
<%= link_to p.group.name, group_path(p.group, #post_iter#{p.id}) %>
This should be good :
<%= link_to p.group.name, group_path(p.group, anchor: "post_iter(#{p.id})") %>
There is a website attr on product_lead table which is optional. If it's present then I wanna turn #produc_lead.lead into a link, but if it's not it should be plain text.
If I use the code below and the website is nil then the link points to the page the user is currently on. If I do it with #product_lead.try(:website), it's gonna be the same. But as I mentioned I would like to have plain text over link in this case.
<%= link_to #product_lead.website, target: "_blank" do %>
<%= #product_lead.lead %>
<% end %>
After playing around I fell back to the following solution, but it's terrible. Any better ideas?
<% if #product_lead.website %>
<%= link_to #product_lead.website, target: "_blank" do %>
<%= #product_lead.lead %>
<% end %>
<% else %>
<%= #product_lead.lead %>
<% end %>
Maybe link_to_if if Rails 4
<%= link_to_if(#product_lead.website, #product_lead.lead, #product_lead.website) do %>
#product_lead.lead
<%= end %>
You can create custom view helper for this.
Well, link_to is going to generate a <a> tag, whether you provide a valid URL or not. So if the URL is nil, yes, it's gonna be a link for you own page.
If you want to "hide" this, you could call a partial in which you place you if/else and so on, but it's just to sweep this under the rug :)
Or if you wanna go further, as #Jovica Šuša, a view helper is the most elegant solution.
I've got a very simple setup of posts with associated tags. When I 'show' a post I want to be able to link to each one of those tags BUT it seems to only link to the tag :id that shares the :id of the post I'm showing.
My code:
<% #post.tag_list.each do |tag| %>
<%= link_to tag, tag_path() %>
<% end %>
Let's say I'm looking at post number 2, the above will only link me to /tags/2 , no matter which tag I click on. I'm sure the answer is embarrassingly simple but it's driving me crazy. Thanks so much.
Pass tag to your route helper:
<% #post.tag_list.each do |tag| %>
<%= link_to tag, tag_path(tag) %>
<% end %>
Update:
Change tag_list to tags:
<% #post.tags.each do |tag| %>
<%= link_to tag, tag_path(tag) %>
<% 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).
I have a list of 'notes', and each note has some tags via acts_as_taggable_on. It's a great plugin, and the tags are working wonderfully.
What would be the best way to filter this list of notes by the tag that is clicked on?
Example:
<% #notes.each do |note| %>
<%= note.content %>
<% note.tag_list.each do |tag| %>
<%= link_to tag, '#', :class => "tag" %>
<% end %>
<% end %>
What should I replace the '#' with in order to change or scope out #notes? Not too familiar with this.
EDIt: I want something just like StackOverFlow actually, how would I add parameters to the URL based on the link?
Thanks!
I believe I figured it out. Was just having an off-moment.
I can create a named route like:
match 'tags/:tag' => 'controller#index', :as => 'tag'
And that way I can get the parameter I need.