I am trying to link to an external site by doing the following:
<%= link_to 'My Link', #link.url %>
What I get when I click 'My Link' is something like:
http://localhost:3000/links/www.facebook.com
What can I do to make this a external link www.facebook.com?
Seems like you saved your url without the protocol in the database (www.facebook.com), so they are being interpreted as relative URLs. Try specifying the protocol.
link_to 'My Link', "https://#{#link.url}"
This should solve your current problem.
But, a better approach would be to save the url with their protocol in the database in the first place.
So, save the url as https://www.facebook.com instead of www.facebook.com.
Addition tip: If you want your external link to open in a new browser window, use :target => '_blank':
link_to 'My Link', "https://#{#link.url}", :target => '_blank'
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.
My requirement is to add a link to mail sent from application developed in Ruby On Rails. Clicking that link in mail need to route user to that particular record in the application.
Can someone please help me with this some sample code.
In your view template use _url. E.g.:
<%= link_to 'Edit User', edit_user_url(#user) %>
it will return the complete URL.
You can use a simple link_to method in the mail or you could use a simple anchor tag. The latter you would have to build up.
If you need information on how to build links and paths check the rails guides. This has an example on the first section of the guide.
e.g. <%= link_to 'Patient Record', patient_path(#patient) %>
Besides this you need to provide more information if you want more help.
If you are starting with Ruby On Rails I would recommend looking at the videos on rails cast.
For your email problem you can look at sending-html-email
Also in the email I like to use the link_to helper like this
<%= link_to 'Click Me', something_url(#user.id), %>
I hope that this works. And Happy Coding
the url comes from a database table, e.g. www.test.com.
How do I get a working link if that url is stored in the database (as 'web site' column).
I tried:
<%= link_to(company.web_site, company.web_site, :remote => true) %>
but I got http://localhost:3000/company_name.com
and I can't seem to get rid of the localhost for the link.
Use <%= link_to(company.web_site, "http://#{company.web_site}", :remote => true) %> Hint it with "http://" string would do the trick.
Or prepend the url with http:// or https:// in your database at the fist place also be an alternative way.
I'm using
link_to 'My link', path(:arg1 => session[:arg1], :arg2 => session[:arg2],:arg3 => anyobject.id), :method => :post
but the HTML link being generated includes (arg1,arg2,arg3) as URL query parameters.
How can remove them? Did I miss something in the documentation?
A link_to will always put the arguments into the query string as it is creating a get-style HTML link - even if you put :method => :post that just appends an extra ("special") argument _method.
What I think you really want is a button_to link - which will make it into a sort of form-post. It works the same, but it says button_to instead (for example, button_to 'My link', path(:params => :go_here). The downside is that it will look like a button. But you can give it a CSS class (eg "unbutton") and then change the styling on that CSS class to make it not look like a button.
Alternatively, if what you really want is actually to have no params passed to the controller at all... then just don't include them when making your link (for example, link_to "My link" path - there's no need for :post if you don't want to post any params).
Finally, if what you want is for the params to become a part of the URL (for example, stuff/[param_1]/more_stuff/[param_2], etc.) then you need to update your routes to include these parameters as options. Have a look at the routing section of the rdoc for how to do that.
You can use below code, which rails.js need data-method to switch to post mode in Ajax.
<%= link_to '<button id="print" type="submit" class="btn">Print</button>'.html_safe, { controller: :lots, id: #lot.containername, action: "print_#{print_template}", remote: true }, {'data-method' => :post} %>
It is possible to redirect_to some url from the controller and open the url in the new window at the same time ?
Not really from the controller action.
A redirect is an HTTP response with a 301/302 status code and an empty body in Rails. As the body is empty, you don't have any way to include extra processing for the browser (such as javascript etc to open a new window).
Assuming you have the the controller action triggered via a hyperlink on another page, your best bet would be to have that open in a new window. Then the redirect target would be displayed.
Either that, or render the same page again as a regular response (no redirect), and insert some javascript when the page loads to open a new window
#madlep "your best bet would be to have that open in a new window"
Thanks for that one. Works perfectly!
view:
<%= link_to "Buy this product", buy_path, target: "_blank" %>
controller:
def buy
...
redirect_to link
end
Searched a little and found a solution... it's not on the controller (as I originally wanted too but it's quite easy...) in your view:
<%= link_to '', {:controller => 'assignments', :action => 'overall_report_gen', :evaluator_relations => #evaluator_relations}, :target => '_blank', :id => 'reporte' %>
<script language="JavaScript">
document.getElementById('reporte').click();
</SCRIPT>
so, with '' as the display string in the link_to, you get an invisible link,
with the :target => '_blank' the link, when clicked will open in a new window
and the :id => 'reporte' is needed in order to click the link with javascript.
so just place the same id in the link_to and in the string in the javascript, and that's it
remember to place the javascript snippet AFTER the link_to so as to wait the element is loaded