social site link that be open in another window in rails app - ruby-on-rails

How to implement social sites link in rails app. Actually I did the following:
<%= link_to "www.linkedin.com/in/raj4057kumar", :target => "_blank", class: "linkedin" do %>
<%= image_tag("linkedin.png", :alt => "Linkedin") %>
<% end %>
But the problem is:
Url being: http://localhost:3000/www.linkedin.com/in/raj4057kumar
while we need like: www.linkedin.com/in/raj4057kumar
How to do this???

Its because you are not adding the http:// infront of the url.
Change your code to:
<%= link_to "LINK NAME HERE", "http://www.linkedin.com/in/raj4057kumar", :target => "_blank", class: "linkedin" do %>
<%= image_tag("linkedin.png", :alt => "Linkedin") %>
<% end %>

Try inserting https:// at the beginning for external url
<%= link_to 'Raj Kumar', 'https://www.linkedin.com/in/raj4057kumar', target: '_blank', class: 'linkedin' %>

Related

How can I display a truncated URL with the link_to helper?

How would I truncate a url submitted from a form? Here is what I am using to display the submitted url:
index view:
<%= link_to gym.gym_website.try(:remove, /.*\/\//), url_for(gym.gym_website), target: '_blank', class: '' %>
How would I add the truncate method onto this? I am currently removing http/https from the submitted url.
<%= truncate(gym.gym_website, :escape => false, :length => 45) %>
Thanks in advance!
You could use the block version of the link_to helper for readability here.
<%= link_to url_for(gym.gym_website), target: '_blank', class: '' do %>
<%= truncate(gym.gym_website.try(:remove, /.*\/\//), :escape => false, :length => 45) %>
<% end %>

how to link form_tag to button rails 4

I have a selection box on my page, and when I click the submit button I want to take the selection choice to the server as either a post or get variable (I don't think it matters). How do I link this form:
<%= form_tag(store_rates_path, method: 'get') %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
to this button:
<%= button_tag(link_to("Get Rates", store_rates_path))%>
You only need to provide the path to the form_for method, to link it to the rates action of your stores controller:
<%= form_tag(store_rates_path, method: "get") do %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select((1980..2014).to_a)) %>
<%= button_tag "Get Rates" %>
<% end %>
In your rates action you can then retrieve the :year parameter passed as follows:
def rates
#year = params[:year]
end
You also need to define the route in your routes.rb file as follows, if you haven't yet:
get 'stores/rate', to: 'stores#rate', as: 'store_rates'
IMPORTANT
Just note that if the rates belong to a specific store, meaning the url is something like stores/1/rate then the above get must be stores/:id/rate, which also means you need to pass the store.id to the store_rates_path in your form: store_rates_path(#store)
You can use rails submit_tag helper
<%= form_tag(store_rates_path, method: 'get') %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
<%= submit_tag "Get Rates" %>
<% end %>
OR
If you want to use a link or button to submit your form parameters then you can use some js magic to achieve it:
<%= form_tag store_rates_path, id: "store-form", method: 'get' %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
<%= link_to "Get Rates", "#", id: "store-form-btn" %>
<% end %>
$(document).on("click","#store-form-btn",function(e){
e.preventDefault();
$("#store-form").submit();
});

Rails: link_to not rendering dynamic text

In my rails 4 erb view i have this link,
<%= link_to "Download #served_file.title", "/public/uploads/#served_file.file", :class => "btn" %>
However, it won't replace #served_object.title and #served_object.file with the text but just placing those lines inside a <%= %> works as expected.
You need to interpolate the values into the string.
<%= link_to "Download #{#served_file.title}", "/uploads/#{#served_file.file}", :class => "btn" %>

How can I make link_to in this case?

I want the link which goes to
http://example.com/shop/:shop_name
so it should be something like this.
<%= link_to "Shop", req.host + "/shop/"+ #shop.shop_name , :class => 'btn' %>
I don't want to use something_path or something_url here.
I just want to create url link from current host, and variable.
How can I?
UPDATE:
<%= link_to "Shop", request.host + /shop/ +#shop.shop_name , :class => 'btn' %>
This takes me to
http://www.example.com/shop/www.example.com/shop/walmart
try with,
<%= link_to "Shop", "/shop/"+ #shop.shop_name , :class => 'btn' %>
Why don't you want to use url helpers? Doing it by hand is error-prone.
Simply put this to routes.rb
get "shop/:name", to: "shops#show", as: "shop_name"
Then you can use this in your templates:
<%= link_to "Shop", shop_name_path(#shop.shop_name), :class => 'btn' %>
In the show action of shops controller just fetch the name param:
shop_name = params[:name]

Rails : Linking an anchor from a different controller/view

<%= link_to (:controller => "company_stuff", :action => "index", :anchor => :menu), :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
I am having difficulty linking a page which is on a different controller and also the link is an anchor. Basically the controller is called company_stuff the action is index and the anchor is called #terms
The problem was that the :controller :action :anchor was not being passed through as a hash, separate from the CSS class
Below is the solution
<%= link_to "Terms Of Use", {:controller => "company_stuff", :anchor => "terms"}, :class => "links" %>
I believe you can try something like this
<%= link_to index_company_stuff_path + "#terms", :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
Or
<%= link_to index_company_stuffs_path + "#terms", :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
Depending on your controller name and route.
You can find more information on this question How to create an anchor and redirect to this specific anchor in Ruby on Rails

Resources