How to get rails3 to do remote link using database info - ruby-on-rails

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.

Related

Call an action on button click in ruby on rails

I have an action defined in my lib directory. The action is used to send a get request in the server. I want to execute the function on button click. How can I do this? I know this is not the right approach. I am new to rails.
show_document.rb (in lib directory)
class Legaldoc::ShowDocument
def view_document(sessionID, token_document)
require 'rest-client'
require 'zip'
res = RestClient::Request.execute(
:method => :get,
:url => "http://myurl.com",
:headers => {
:ldsessionId => sessionID,
}
)
end
end
in my view file
<%= link_to 'Button', '#', :onclick => "view_document(sessionID, token_document)" %>
I know this is confusing when you start developing in rails, but you have to consistently be very aware where your code is running. Your ruby code runs on the server, your html/js runs in the browser. Rails "prepares" html/js, but when you click a button, and you have to ask something from the server, you will have to use a url to acces a/any server.
Or you could resort to using javascript on the client directly, to fetch the data.
But since you already have the code in lib you will have to
add an endpoint, aka an action in a controller (add a new controller or add a new action in an existing controller, that is a bit hard with so little background information)
add a route to that controller/action
add the controller-code to call your lib function and format the answer in such a way relevant for the user
then add the button as follows:
<%= link_to 'Button', my_action_controller_path(session_id: sessionId, token_document: token_document) %>
(the name of the path depends on the names of controller/action, and you can see the names of the routes by running rake routes)
However your code seems to just fetch a page at myurl.com, so you could also just build a link to myurl.com ?
<%= link_to 'Button', 'http://myurl.com', target: '_blank' %>

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'

Safe way to load url stored in database?

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.

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

Does link_to automatically pass :id when controller is specified?

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.

Resources