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 "/..."
Related
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 %>
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.
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 build a simple app using Ruby on Rails. Essentially, I have a route that maps to a controller, whose view looks like this:
<div class="wishlist-container">
<% #wishlists.each do |w| %>
<div class="wishlist-card">
<h4><%= w.title %></h4>
<%= link_to "View List", wishlist_path(w) %>
</div>
<% end %>
Everything works correctly except for the link. For whatever reason, the link links to "." instead of "/" where <id> is the id. For example, it should link to /wishlist/1 but instead goes to /wishlist.1.
What is happening? How can I solve this problem?
For using paths helpers in your code , you should specify the resources , not only get or post in your routes.rb
for example if you've
get 'wishlist/:id' It may not work .To make your path work you should specify
get 'wishlist/:id', to: 'wishlist#show', as: 'wishlist'
For more information read Ruby docs and This article
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 %>