What is the best way to go about getting embedded HTML in the body of a link generated with the link_to method?
I basically want the following:
This is a <strong>link</strong>
I have been trying to go about this as suggested in Rails and the <span> tag but with no luck. My code looks like the following:
item_helper.rb
def picture_filter
#...Some other code up here
text = "Show items with " + content_tag(:strong, 'pictures')
link_to text, {:pics => true}, :class => 'highlight'
end
item_view.html.erb
#...
<%=raw picture_filter %>
#...
Try it this way
<%= link_to(raw("a <strong>strong</strong> link"),{:pics => true},{ :class => 'highlight'}) %>
= link_to "http://www.example.com" do
<strong>strong</strong>
As of 2016 I prefer this method.
<%= link_to my_path do %>
This is a <strong>ape</strong>
<% end %>
you can use html_safe
<%= link_to ("<i class='someIcon'></i> Link").html_safe %>
Not sure if this is the best way.
But I have been very successful in staking alot of the view helpers inside the content_tag call.
It also might not hurt to call a .html_safe
link_to(content_tag(:span, "Show yada " + content_tag(:strong, "Pictures")), {:pics => true})
Related
I feel like there's no easy way to do this in rails, but since I'm fairly noob in rails I decided to ask for solutions:
I have a form in a view that contains a single (text) input. How can I specify the form url such that it will do a GET to /something/<input> ?
I know I could:
use custom javascript code
post to an endpoint that would do the redirect
Is there any cleaner way?
(using rails 5.2.1 if relevant)
I think you could use something like this:
<%= form_tag(route_path method: :get) do %>
search <%= text_field_tag :search %>
<%= submit_tag 'Search' %>
<% end %>
It depends on which form helpers you use.
Maybe you can try this
In your form:
<%= form_with(url: "path_name/input_field_name", method: :get, local: true) do |f| %>
<%= f.text_field :input_field_name %>
<%= f.submit 'Search', input_field_name: nil %>
<% end %>
In your controller:
unless params[:input_field_name].blank?
#results = ModelName.where('input_field_name iLIKE ?', "%#{params[:input_field_name]}%")
end
I'm looking to include a link in a form label as such:
<%= form.check_box 'eula' %>
<%= form.label 'eula', "I agree to the <a href='#' id='eula-link'>EULA</a>", class: 'label-checkbox' %>
Rails writes the HTML out, as it probably should, but how would I accomplish this? Clicking EULA opens a JS popup. I was thinking of embedding a link_to in there somehow ?
Using html_safe with parens will render the html, like so:
<%= f.input :eula, :as => :boolean, label: ("I agree to the #{link_to 'Terms of Service', terms_path}.").html_safe %>
Assuming you're using vanilla rails form helpers, you can do this:
f.label :eula do
'I agree to the #{link_to("EULA", "#")}'
end
Source: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-label
19 ways tried, with either the hyperlink being encoded or html_safe replacing hyphens in the url ???
This is what worked for me
<%= f.label :cookies do
"Do you agree to our #{link_to('Cookies Policy', 'http://www.your-url.co.uk/privacypolicy')}".html_safe
end %>
The specific use of " and ' appears significant.
The answer from jenson-button-event nearly worked for me, but required a change in the placement of a parenthesis close to load without errors.
For me the below solved it. Note the close-bracket after 'Cookies Policy' here, rather than after the link path itself.
<%= f.label :cookies do
"Do you agree to our #{link_to('Cookies Policy'), 'http://www.your- url.co.uk/privacypolicy'}".html_safe
end %>
try "I agree to the #{link_to 'EULA', #, :id => 'eula-link'}"
I wanted a simple way to add a font awesome help link button after the form field label, this is what I used in my haml file:
= form_for [#preplan, #structure] do |f|
= f.label :template do
Template
= link_to 'https://intercom.help/blazemark/preplans-and-structures/structure-templates', target: '_blank' do
= fa_icon 'fw info-circle'
As of rails 6.0.2.1 (January 2020), this is what worked for me:
<div class="form-group form-check">
<%= form.check_box :accept_terms, class: "form-check-input", required: true %>
<%= form.label :accept_terms, class: "form-check-label" do %>
<span>
Accept <%= link_to 'Terms and Conditions', 'https://your.url.here.com' %>
</span>
<% end %>
</div>
I'm using link_to in RoR 3
When I use it like this, it works fine:
<%= link_to "Add to your favorites list",:controller =>
'favourite_companies', :action =>'create',
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}" %>
But I would like to pass in a class as well
however, this is not working for me. The class works, but it breaks the link. Any ideas?
<%= link_to "Add to your favorites list",{:controller =>
'favourite_companies', :action =>'create'},
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
:class=>"ui-button-text button_text"} %>
<%= link_to "Add to your favorites list",{:controller =>
'favourite_companies', :action =>'create'},
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
:class=>"ui-button-text button_text"} %>
try this
<%= link_to "Add to your favorites list", :controller =>
'favourite_companies', :action =>'create',
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
{ :class=>"ui-button-text button_text" } %>
Since the :class should be in :html_options (refering to API)
link_to(body, url, html_options = {})
The proper way of doing what you have is as follows:
link_to "Foo", { URL_FOR PARAMS HERE }, :class => "bar"
As far as setting the controller and action manually like this, well, it's crap. Rails builds url helpers for you; use them and save yourself some time, energy, and add clarity, all at once:
link_to "Foo", favourite_companies_path(#company), :method => :post
What you're doing with the string interpolation is a bad idea too…it's just wasteful and cluttered for no reason at all. The following is the same, just better:
link_to "Foo", :company_id => #company.id, :company_name => #company.name
As far as why your link wasn't working, if wrapping it in a div helped it sounds like you have a problem with your HTML structure, not the link_to syntax.
I'm using a link_to do-end block so the above previous solutions didn't work for me.
If you want to embed other tags in your a tag, then you can use the link_to do-end block.
<%= link_to favourite_companies_path(:company_id => #company.id, :another_url_param_here => "bar"), { :class => "ui-button-text button_text", :title=> "We can have more html attributes as well" } do %>
<i class="fa fa-star"></i>
<%= #company.company_name %>
<% end %>
In this case it's
<%= link_to path(url_params), html_options = {} do %>
<% end %>
Be careful because in Rails 5 the above methods will still result in a wrong URL generation. The controller and action need to be put in a literal hash in order for it to work in Rails 5. What you will have should be something like this
<%= link_to "Add to your favorites list",
{ controller: "favourite_companies", action:"create"},
company_id: #company.id,
company_name: #company.company_name,
class: "ui-button-text button_text" %>
This doesn't give the full URL, which breaks as it's in a mailer
<%= link_to #conversation.title, conversations_path %>
This gives me the full URL, which is good:
<%= conversations_url(:only_path => false) %>
How do I get the best of both worlds? I want to link_to but have the full path?
Thanks
Without testing this.. Have you tried this?
<% conversations_url(:only_path => false) do %>
#conversation.title
<% end &>
Thanks Oluf, ran into your answer when I was trying to fix a similiar problem.
The link may be rewritten into:
<%= link_to( #conversation.title, conversations_url( :only_path => false)) %>
I'm fairly new to Ruby on Rails, and I'm attempting to create some fancy CSS buttons using the "sliding doors" technique. I have it almost working, but I feel like there has to be a better way to handle the tags for a link.
The way I'm currently doing it:
<%= link_to '<span>New car</span>', {:action => "new"}, :class=>"button" %>
This isn't terrible, per se, but I would like to know if this is the best way to handle span tags in RoR.
Another option is this:
<%= link_to content_tag(:span, 'New car'), {:action => "new"}, :class=>"button" %>
docs
Or you could be pro and use named routes/resources + Haml. That would make it look like:
%a{ :href => new_car_path }
%span New Car
What you have is fine though..
If you're still curious, here are some ways to rewrite your code:
Use content_tag:
<%= link_to content_tag("span", "New car"), {:action => "new"}, :class=>"button" %>
Use link_to with a block:
<%= link_to {:action => "new"}, :class=>"button" do %>
<span>New card</span>
<% end %>
And of course, you can combine the two by putting a content_tag inside the block, but I'll leave it to the reader as an exercise :)