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.)
Related
I need to separate unique ids for some linking purpose, I just want to know how to make an unique id for link_to?
Like I am using:
<%= link_to "Edit", edit_question_path(question.id),method: :get, :remote => true, :id => #question.id%>
This didn't work then I tried:
<%= link_to "Edit", edit_question_path(question.id),method: :get, :remote => true, id: myBtn_<%=question.id%> %>
Nothing worked. Is there any solution?
Try with:
<%= link_to 'Edit',
edit_question_path(question.id),
method: :get,
remote: true,
id: "myBtn_#{question.id}" %>
You need to see what's the way to access a question variable, as instance or local one (I used a question, as you're doing with the path), and myBtn_<%=question.id%> won't work if you don't have defined a myBtn variable, even less if you try to include erb tags to print inside the erb open-close for the link_to.
I know that you can add HTML classes to link_to helpers like this:
<%= link_to 'Logout', logout_path, class: 'primary-link-style' %>
But what about the mail_to helper? There doesn't seem to be a way to add a HTML/CSS class like you can with the link_to helper.
The docs only talk about examples of inline styling which I want to avoid.
The method signature from the docs you linked to:
mail_to(email_address, name = nil, html_options = {}, &block)
That third argument, html_options, works just like the same argument of link_to:
<%= mail_to "foo#example.com", "Email me", class: "primary-link-style" %>
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" %>
I think there is a relative easy answer to this, but how can I make this link_to...:
<%= link_to(image_tag(p.thumbnail, class: "hoi"), "Something in here?") %>
...link to the same as this:
<a class="fg" href="#<%= p.id %>" data-toggle="modal">
?
(Just to be clear: When I click on the image in the link_to, I want to get linked to href="#<%= p.id %>" with the data-toggle="modal" option).
If you're trying to link an image to an anchor on the page, do this:
<%= link_to(image_tag(p.thumbnail, class: "hoi"), "##{p.id}") %>
You can do normal ruby string interpolation inside erb block (<%= %>)
Try something like this:
<%= link_to "##{p.id}", "Something in here?", :class => "fg", :data-toggle => "modal" %>
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.