Add CSS class to mail_to ruby on rails helper - html

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" %>

Related

Nest a link_to inside of another link_to

I need to nest an 'a' element inside of another 'a' element like so:
<a href="www.example.com" class="so-and-so">
<span>something</span>
<i class="an-icon">
</a>
However I need to use the rails 'link_to' helpers instead of plain html. But when I try to nest the link_to helper like this:
<%= link_to "www.example.com", class: "so-and-so" do %>
<%= content_tag :span, "something" %>
<%= link_to "www.another.example.com", class: "another-link" do %>
<%= content_tag :i, class: "an-icon" %>
<% end %>
<% end %>
It outputs html more like this:
<a ...>
<span ...>
</a>
<a class="another-link" ...>...</a>
Any help would be greatly apreciated. Thank you!
EDIT
It turns out that rails was outputting the correct html after all, but the browser is not interpreting it correctly. It it possible to have a link inside of a link?
SOLVED (ISH)
It turns out that nesting <a> elements is not valid HTML, so the browser was just trying to "correct" my code. I ended up just placing the a element outside the other one and using CSS to make it look the same.
Since you need the inner link tag point at a destroy action on a controller. You can trigger the action by the click event of the first link.
$('#first_link').on('click', function() {
$.ajax({
type: "DELETE",
url: YOUR_SECOND_LINK ,
data: {id: YOUR_ID },
success: function(data, textStatus, jqXHR){...},
error: function(jqXHR, textStatus, errorThrown){...}
})
});
This can be achieved by using the <template> element which will prevent the browser from closing an anchor tag before another one is opened.
<%= link_to "www.example.com", class: "so-and-so" do %>
<%= content_tag :span, "something" %>
<template>
<%= link_to "www.another.example.com", class: "another-link" do %>
<%= content_tag :i, class: "an-icon" %>
<% end %>
</template>
<% end %>
I worked this out because I was refactoring Vue components that had nested anchor tags into html.erb files. It is a requirement of each of the individual Vue components that it is wrapped in a <template> tag so I copied it over and voila!

link_to helper weird url

Hello everyone I was wondering how I can get rid of this ugly url that was generated when I clicked a link on my website. It looks like this:
http://www.imaaroncs.com/about?as=AboutMe&class=changeMe&controler=welcome
I want it to just say about but I do not know how to do it with the link_to rails helper this is what I have so far:
<li class="navbar-brand"><%= link_to "About Me", class: "changeMe",controler: "welcome", as: "AboutMe", action: "about"%>
You're sending a few parameters you don't need (the ones after ? are being interpreted as additional query params)
<%= link_to "About Me", action: "about" %>
Try using the literal hash:
<%= link_to "About Me", {class: "changeMe", controller: "welcome", as: "AboutMe", action: "about"} %>
PS. Also, there is double l in controller. You have one.

Rails erb get value of a text_field_tag

I have simple html.erb form like below
<table>
<%= text_field_tag :tesintg %>
<th><%= button_to 'Apply now', apply_product_index_path(:value => "Want Value of input in text field here") , method: :post %></th>
</table>
When "Apply now" button is pressed I want the value in the testing text_field_tag to be posted as query params as {"value" : "Value in the text field"}
How do I achieve this.
I think the best way to do something like that is just to create form_tag
<%= form_tag apply_product_index_path, method: :post do %>
<%= text_field_tag :teasing %>
<%= submit_tag %>
<% end %>
This will pass to your controller hash params: { teasing: 'value passed as teasing }. You can easily use it from there with params[:teasing].
You don't need to grab value from text_field_tag and put it into button.
Also remember that if you are creating new object, very ofter preferred way is to use form_for tag which uses specific model. I'm not sure what are your intentions, so i'm not going to rewrite everything that has already beed said. You can read much more in here: http://guides.rubyonrails.org/form_helpers.html

Using a rails delete method in an html link?

I need to change this to HTML and have no idea how to put the delete route in a normal a tag, any ideas? Thanks
<li><%= link_to "Link", user_photo_pin_path(user_id: #user.id, photo_id: #photo.id, id: {{id}} ) , method: :delete, data: { confirm: 'Quieres borrar esto?'} %></li>
You can use something like "pry-rails" or "debugger" gems. Set up "debugger" method in your view and execute any of ActionView:Helpers like link_to or form_tag and see what html returned by this methods.

rel attribute in <a> in Rails 3 erb 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.)