I'm trying to link to an external page in Rails. I've seen several questions about this but none of the solutions are working for me.
I believe this is because the url is a subdomain (i.e. http://pages.foo.com/contact.html). This link is directing to http://www.foo.com/#!http://pages.foo.com/contact.html instead.
My link_to looks like this:
<%= link_to 'contact', 'http://pages.foo.com/contact.html', :class => 'contact pNav' %>
Any help greatly appreciated. Thanks!
According to rails apidock
<%= link_to 'contact', 'http://pages.foo.com/contact.html', :class => 'contact pNav', :target => "_blank" %>
Related
How can I put link_to redirecting to root_path in this HAML code?
.icon-bar.large.vertical.four-up
%a.item
%img{:src => "/svgs/fi-home.svg"}/
%label Home
A quick google search lead me to this, https://rivalfox.com/blog/haml-best-practices/
So I would give the following a try,
= link_to root_path, class: ['btn', 'btn-default'] do
= image_tag 'fi-home.svg'
Home
.icon-bar.large.vertical.four-up
= link_to root_path, class: ['item'] do
= image_tag "/svgs/fi-home.svg"
%label Home
This works, thanks for help.
At the top of the user's profile I have this button:
<%= button_to 'Make a detailed enquiry', user_path(#user), :class=>"enquirySection", :method => :get %>
Then at the very bottom of the page I am rendering a form in which the viewer can make an enquiry:
<div class="enquirySection">
<%= render "enquiries/form" %>
</div>
I want the user to be scrolled down to this form when (s)he clicks on the button so I added the ":class=>"enquirySection"" and "div class="enquirySection"". It doesn't work.
I tried something similar to that but I think it does not related to my case.
Any clue of how is this done in Rails with button_to?
Thank you.
You don't need a class, but an id
try
<%= button_to 'Make a detailed enquiry', user_path(#user, anchor: :enquirySection), :method => :get %>
or simply
<%= link_to 'Make a detailed enquiry', user_path(#user, anchor: 'enquirySection) %>
and then
<div id="enquirySection">
<%= render "enquiries/form" %>
</div>
would do the job.
My link_to looks like this:
<%= link_to image_tag(user_likes_selection.page_picture, :image_id =>
user_likes_selection.id, :controller => :preferences_controller,
:action => :checked_average_with_profile) %>
My controller, preferences_controller, has a method called checked_average_with_profile, which, as far as I can tell, is not being called when I click the image.
The html code that is generated from the link_to is
<img>
<a href="/preferences"><img action="checked_average_with_profile" alt="Soul_surfer_film"
controller="preferences_controller" height="70%" image_id="3254"
src="/assets/soul_surfer_film.jpg" width="70%" /></a>
</img>
Why isn't the controller code executed when the image is clicked?
in cases like these, it's easier to read the code if you use the block form of link_to
<%= link_to { :image_id => user_likes_selection.id, :controller => :preferences, :action => :checked_average_with_profile } do %>
<%= image_tag(user_likes_selection.page_picture %>
<% end %>
in your routes, you can also pass an as option so you can use a named route. assuming your routes looks like
match '/preferences/checked_average_with_profile/:image_id' => 'preferences#checked_average_with_profile', as: :check_average_profile
you can simplify your link using
link_to image_tag(user_likes_selection.page_picture), check_average_profile_path(user_likes_selection.id)
Here is how i do in my code.
<%=link_to(image_tag(user_likes_selection.page_picture), check_average_profile_path(user_likes_selection.id)) %>
Try:
<%= link_to image_tag(user_likes_selection.page_picture), url_for({:controller => 'preferences_controller', :action => 'checked_average_with_profile', :image_id => user_likes_selection.id}) %>
Put your paren after user_likes_selection.id, not at the end. You're mixing image tag properties with your link_to properties.
Try:
<%= link_to image_tag(user_likes_selection.page_picture, :image_id =>
user_likes_selection.id), {:controller => :preferences,
:action => :checked_average_with_profile} %>
Finally solved my problem by adding a collection with my action in resources:
resources :preferences do
collection do
get 'save_new_scores_to_profile'
get 'checked_average_with_profile'
end
end
Then, I modified my view code so that I could pass the image_id variable along to the controller.
<%= link_to image_tag(user_likes_selection.page_picture,
checked_average_with_profile_preferences_path(:image_id =>
user_likes_selection.id) %>
In my controller, I made sure to grab the image_id with params and put a redirect_to at the end:
def checked_average_with_profile
params[:image_id]
redirect_to preferences_url
end
If you have this problem, the key parts are passing the id (whatever that may be) within parenthesis of the controller path you specify and using a COLLECTION instead of a MEMBER in your routing file.
I want to know that is there any rel attribute in <%= link_to_peramlink %> like tag in html has.
For example:
in html
my link
I have <%= link_to_permalink article,"con", nil, "my link", "5m" %> in rails. Is there any rel attribute in that?
link_to_permalink is not a standard Rails helper. A simple:
<%= link_to article.title, article %>
(you might have to adjust article.title) will get you a link to the article, assuming something like resources :articles is defined in routes.rb.
Setting a rel attribute is trivial:
<%= link_to article.title, article, :rel => 'nofollow' %>
or other attributes:
<%= link_to article.title, article, :rel => 'nofollow', :class => 'my-css-class' %>
Finally, I don't know what your link_to_permalink helper does, but you can use to_param to make seo-friendly permalinks: see here. (This is the "Rails Way," if you know what I mean.)
I was wondering about how i can do this in rails
because i want something like
<a href="requests/13#new">Comment!<a>
anyone knows it?
greetings
<%= link_to "Comment!", url_for(:controller => "requests", :action => "show", :id => 13, :anchor => "new") %>
If you are working with a Request object and a restful route.
<%= link_to "Comment!", request_path(#request, :anchor => "new") %>
More details are available in the link_to helper documentation.