I have a Link resource. In my view, I have a series of links that people can click to open another web page:
<%= link_to link.description, "http://#{link.url}", :target => '_blank' %>
If the value of #{link.url} is www.google.com, the link works fine. However, if the value of #{link.url} is http://www.google.com and I click it, it will go to the address http//www.google.com <--- notice it's missing a : after http. Can someone help me remedy this?
Thanks!
How about you add a helper method that checks to see if link.url contains 'http://' or not.
You could use something like this:
def link_formatter(url)
prefix = "http://"
url.include?(prefix)? url : prefix + url
end
That will check to see if link.url contains "http://", if it doesn't it will return the proper format for your url, if it does, it will just return link.url as is.
Related
What is the safest way to load a url saved in database?
window.open("<%=raw #post.url%>");
Doesn't seem to be a good idea.
Since window.open opens a link in new tab there is no direct way to instruct the browser form server side to open a link in a new tab.
But you can apply a little JS hack. In your erb file do it like.
<%= link_to "Post", #post.url, {:target => '_blank', :id => "linktoclick"} %>
<script>
document.ready = function(){
document.getElementById("linktoclick").click();
}
</script>
How are you storing the URL in your database/where are you going to use it?
Assuming #post.url will return the url you want, then something as simple as:
[awesome link name]
html output would be:
[awesome link name]
Could you provide more information on where/how the url needs to be used?
<% unless #post.url.blank? %>
<%= link_to #post.url, 'http://' + #post.url, :target => '_blank' %>
<% end %>
You may want to use this. Unless checks if URL in database is empty or not. If URL is mandatory in database, you may want to remove that part.
Extra, if you need validation for url, you may want to use a validation something like this:
URL = /\A(www.)?([a-zA-Z0-9]+).[a-zA-Z0-9]*.[a-z]{3}.?([a-z]+)?\z/
validates :url,
format: {with: URL, message: 'Invalid url format. Make sure you don\'t have http:// in your link'},
allow_blank: true
So, user will input url without http://, and you will add that http:// in html, as I showed above.
I'm building an app where I set links dynamically through a url parameter. I can't figure out how to make the link_to work with both a dynamic link and further url parameters.
TemplateController
def next
#template = Template.find(params[:t])
end
Next View
<%= link_to "#{#template.firstpage_link}(:t => #template.id, :prt => 1)" do %><%end%>
This is what it gives me:
http://localhost:3000/role/step2_path(:t%20=%3E%20#template.id,%20:prt%20=%3E%201)
I've tried a bunch of ways and I get either errors or this link
What you seem to be shooting for is something like
<%= link_to public_send(#template.firstpage_link, :t => #template.id, :prt => 1) do %>
public_send lets you call a public method by passing in its name as a symbol or string.
However, there may be more elegant ways to achieve this with the Rails router, as #Typpex is suggesting. If nothing else, you could clean up the view a bit with something like this in a helper:
def template_path(template)
public_send(template.firstpage_link, :t => template.id, :prt => 1)
end
And then calling that from your view.
I think you are not using link_to correctly, if you look at the link_to API
You will see that the first parameter is what you would like to be displayed and the second one is the rails path. You should pass your parameter when defining the rails path (or plain url) such as
link_to "display text", "#{#template.firstpage_link}?t=#{#template.id}&prt=1"
it would be better if you could use a rails route like
template_path(#template, prt: 1)
In my rails app, each supplier has a website address which is displayed on their supplier show page (value is #supplier.website).
If the website url begins with http:// I'd like to remove this from the view page i.e. rather than displaying http://www.google.com it would just display www.google.com (but the actual #supplier.website will remain unchanged).
Hope that makes sense, and that someone can help...Thanks
You can do that with a URI parse.
uri = URI.parse 'http://google.com/whatever'
uri.host #=> 'google.com'
uri.path #=> '/whatever'
Best to make a helper
def website_link_text(uri)
URI.parse(uri).host
end
And then in the view:
<%= link_to website_link_text(#supplier.website), #supplier.website %>
I am using <%=link_to "Add fund",addFund_item_path(:id=>item.id),:class=>"addfund btn btn-primary"%>" to generate a link to a path in rails.
I want to get only the url (the href) of the path so I can use it in a form. How do i do that?
Just addFund_item_path(:id=>item.id).
So you just want the actual href? I would do this:
<%= content_tag "a", addFund_item_url %>
It won't be a 'link' per se - it will be clickable, but won't go anywhere - but it will show the full href path
I have a link_to code
<%= link_to "#{(pages_counter/2) + 1}", { controller: "videos", action: 'videos_navigate', offset: pages_counter }, remote: true %>
When clicking on the link it's passing the id of the video automatically meaning I didn't explicitly pass a video object or an id via the link_to code. Is this happening because I directly identified the controller and the action as seen in the code above? Thanks in advance
sample URL generated: /videos/videos_navigate/1?offset=2
If you are on a show page, where the ID is in the URL already, and your link_to doesn't specify an ID, it will pick up the ID from the URL . The same thing would happen if you did not specify the controller, it would instead grab the current controller you are in.
So if you went to record 2 and click the same link, your URL will be /videos/videos_navigate/2?offset=2
Why don't you link directly to the route instead. As in, run rake routes int he console, it should print out an path name for your videos_navigate path... then you can presumably link to it like this:
<%= link_to "#{(pages_counter/2) + 1}", videos_navigate_path, :remote => true %>
If it doesn't already have a path, then you can give it one by adding :as => 'videos_navigate' to your route declaration for the action inside your config.routes.rb file. Read here for more information on routing and paths.