Safe way to load url stored in database? - ruby-on-rails

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.

Related

How to link to external link ruby on rails?

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'

Link_to RoR with multiple alert box

(New to RoR)I have a link_to in my erb file.
<%= link_to 'Delete', workflows_path+"/destroy/#{workflow.id}?folder=#{#folder_id}", :confirm => "Are you sure you want to delete '#{workflow.name}'?"%>
This will prompt once asking the confirm message.
I want to prompt another alert box on Confirm(On pressing OK) asking "Do you want to delete sub-folders? " with a 'Yes' or 'No' option and pass it to my controller as a boolean(or any type)
(If they choose no, I would redirect the subfolders to parent directory)
Is there an easier way to do this?
in order to have a double-confirmation you would need some javascript.
give your link a CSS class, like 'double-check-workflows'
then add a javascript to your page
$(document).ready(function(){
$("a.double-check-workflows").on('click', function(){
if confirm("first thing to ask"){
target_url = $(this).attr("href")
if confirm("second thing to ask"){
target_url = target_url+"&your_other_param=1"
}
window.location = target_url
}else{
return false
}
})
})
remove the :confirm option from the link helper though cause the js wil handle that
edit
you should really use the routes helper to create the full url instead of adding params as a string as you posted
you could then add a 'delete_sub_folders' param that is default to false/0 and then replace it in the js with .replace() to set it to true/1 if second confirmation returns true

Missing character from link Ruby on Rails

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.

How can I create link to the json-view of an object in Rails?

What do I need to change in the code below to make it link to a view rendered as json?
<%= link_to 'JSON Link', #mymodel %>
So I'd like to generate the following url
http://localhost:3000/mymodels/1.json
instead of
http://localhost:3000/mymodels/1
(These urls both work as expected.)
To do this, you must specify the format:
<%= link_to 'JSON Link', your_model_path(#mymodel, :format => 'json') %>
The URL helper methods can be retrieved by running:
rake routes
The first column is the name of the helper method, on which you should append either _path or _url, the latter will generate an absolute URL.
More information is in the Guide To Rails Routing

How to get rails3 to do remote link using database info

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.

Resources