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.
Related
I have the following submit_tag button
<%= submit_tag("Save Email".upcase, name:"email_change","data-target":"email_change.submit") %>
I am trying to have it redirect to a new view, similar to what is done with using link_to. Is there a way to do something similar using the submit button?
You can do:
<% link_to some_path do %>
<%= submit_tag("Save Email".upcase, name:"email_change","data-target":"email_change.submit") %>
<% end %>
The 'Rails' way to do this would probably be to wrap the redirect in some conditional logic in your controller action, especially if you're submitting data from a form
I've been playing around with my if and else statement but it's always just providing one.
First try
<% if #albumable == #user %>
<%= link_to "Edit", edit_community_album_path(#albumable, album), class: "album_edit" %>
<% else %>
<%= link_to "Ediiiit", edit_user_album_path(#albumable, album), class: "album_edit" %>
<% end %>
Second try
<% if #community == #community_id %>
Albumable will either be a user_id or community_id. When I play around with the code I only get 1 result from the two for both the user album edit page and the community album edit page. I don't understand how I'll be able to making an if statement if it isn't within the community page, it should produce the else statement. All help is appreciated, thank you.
I would suggest refactoring this code to remove the if/else statement altogether. For example, the following provides the exact same functionality:
<%= link_to "Edit", [:edit, #albumable, album], class: "album_edit" %>
You can read more about this at http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects
Compare the id values instead. Depending on how you loaded those variables with records, Ruby might not think they're equal.
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'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 write something like:
<% if signed_in?.blank? %> or <%= link_to "Sign Up", sign_up_path %>
What is the difference between the two signs of <% and <%=?
Why make it this way instead of using just one for simplicity?
When do I know I need to use <% over <%=?
<%= puts the return value of the code inside to the page.
<% just execute code.
Here is the good guide about ERB http://api.rubyonrails.org/classes/ActionView/Base.html
<% %> Simply executes the statement(s) inside that block, whereas <%= %> will output the result of the statement.
So for example, with the <% if signed_in?.blank? %>, the ruby interpreter just executes that code and checks if signed_in is blank.
The <%= link_to %> statement will actually generate HTML.