How to put %span inside of =link_to helper? [duplicate] - haml

This question already has answers here:
put haml tags inside link_to helper
(3 answers)
Closed 8 years ago.
= link_to t('actions.convert_to_template'), convert_backup_path(backup), {:title=> t('.convert_backup'), :class => 'icon convert_to_template'} if backup.built? && authorized?(backup, :update)
How do I put %span t('actions.convert_to_template') inside of this link_to

make a helper, like this
def convert_to_template_text
html = content_tag :span, t('actions.convert_to_template')
html.html_safe
end
or use block with link_to

Related

Is it possible to render partial using link_to helper?

I have a Rails 3 application with simple navigation that contains links to controllers actions using link_to helper.
Now, I need to add a link which will refer to partial. Is it possible to combine the link_to and render: partial methods?
Generally, what I need is to combine somehow the following codes and have a link_to which is rendering the partial:
<%= link_to 'List', security_users_path , remote:true, class:'e-icon e-icon-camera' %>
<% #security_user = SecurityUser.new %>
<%= render partial: 'security_users/sign_in', :locals => {:security_user => #security_user} %>
If it is not possible, what other alternatives I have if I want to render a partial?

link_to Helper Modifications

Inside of a rails app that I am working, I modified the link_to helper slightly:
def link_to(*args, &block)
args[1] = params[:client_id].present? ? "#{args[1]}?client_id=#{params[:client_id]}" : args[1]
super
end
I did this so I wouldn't have to add the :client_id => params[:client_id] every time I wrote a link_to inside of the app. Well, I have kind of pigeon holed myself with the following problem...
If I have this link_to:
<%= link_to "Continue to billing info", add_product_path(:product_id => #product.id), :class => 'btn' %>
Using my link_to helper creates a link, like so:
http://localhost:3001/orders/add_product?product_id=35?client_id=HT274848772
I am at a slight loss on how to modify my helper so that the link will work as normal while including the :client_id param...
You want to add your parameter to the link url, not to the link itself. Maybe you should rewrite the url_for helper, which is the helper used by all of the url helpers ( http://apidock.com/rails/ActionView/Helpers/UrlHelper/url_for )

Rails simple_form pre-populated input values lost during server side form validation [duplicate]

This question already has an answer here:
Rails simple_form server side validations lost URL params
(1 answer)
Closed 8 years ago.
I have a simple_form in my rails app that I want to pre-populate
some of the fields with URL parameters.
Example URL is:
http://localhost:3000/surveys/new?phone=8675309
This pre-populate the phone field corretly using this code:
<%= simple_form_for #survey, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :state, :required => true %>
<%= f.input :zip, :required => true %>
<%= f.input :phone, :required => true, :input_html => { :value => params['phone'] } %>
<% end %>
The problem is if the form is submitted without a required field
the form is reloaded but the phone input value is not retained.
If the form is submitted without a value for zip
the reloaded page with validation errors in red URL is now:
http://localhost:3000/surveys
If, say, the state field is correct but no zip code the form is reloaded
with an error msg saying zip is required.
The state value is retained however the phone is not.
I guess it has to do with :value => params['phone'] in the f.input for the phone.
Is there anyway I can populate the simple_form with URL paramenters
on it's initial load and have those value retained if the serverside validation fails?
Many thanks.
Remove the :value from your view:
<%= f.input :phone, :required => true %>
and use this URL:
http://localhost:3000/surveys/new?survey[phone]=8675309
That should generate the "standard" survey parameter hash expected by the controller. In my test, that works the same as if the user had typed in the value, with the usual validation handling.
Inside the controller, the hash is called params[survey] with individual parameters represented as params[survey][phone], params[survey][zip], etc.
With the help of this answer, I found that you can generate the URL with the link_to signature link_to(body, url_options = {}, html_options = {}):
<%= link_to 'New survey', { :controller => "surveys",
:action => "new",
:survey => { :phone => "8675309", :zip => "10001" } },
{ :target => "_blank" } %>
Note that url_options is the first hash, and within that hash you have a survey hash for pre-loading values. Finally comes the optional hash of html_options (where I added target="_blank" for illustration purposes).
Thanks Mark,
I re-posted this question again a while back and it was answered corretly here:
Rails simple_form server side validations lost URL params

rails 3 link_to with nested content_tag to create <a href with nested span - how to?

Hi I got a noob question, I want to create the following HTML result:
TEXT<span class="arrow-big"></span>
In the above HTML I want to have text with a span-class to style in an image via css.
When I try the following implementations the result reflects just one part of the needed implementation:
<%= link_to "TEXT", controller_path, :class => "button-big layer" %>
results in:
TEXT
and
<%= link_to(content_tag(:span, "", :class => "arrow-big"), controller_path, :class => "button-big layer") %>
results in:
<span class="arrow-big"></span>
Does anyone know how to accomplish?
You could also nest tags by using alternative syntax for link_to helper
<%= link_to controller_path, :class=> "button-big layer" do %>
Text
<%= content_tag(:span, "", :class => "arrow_big" %>
<% end %>
Simply concatenate your text with the 'span':
<%= link_to(("TEXT" + content_tag(:span, "", :class => "arrow-big")).html_safe,
controller_path,
:class => "button-big layer") %>
You'll need the .html_safe around the concatenation since the + operator will otherwise escape the HTML of the content_tag.
Reading your question I did solve my problem.
Than I propose another way to answer your question.
You could create a helper method to make this kind of link that you need.
It would be something like this
def link_with_text_and_span(href, text, span_options= {}, link_options = {})
span_tag = content_tag(:span, span_options[:content] || '', :class => span_options[:class] || '')
link_to(span_tag, href, :class => link_options[:class] || '')
end
The good about it is that your view will be cleaner.
Then you can just call this helper method in your view
<%= link_with_text_and_span("/controller/action", "TEXT", {class: 'arrow-big'}, class: button-big) %>
PS: This code can be improved for sure, if other users want to, please do it.
Here's another way you could use without the content_tag. Not the cleanest but it works!
<%= link_to '<span class="arrow_big"></span>'.html_safe, controller_path, class: "button-big layer" %>

Rails 3 - How to send data on link_to :remote=>true?

I am trying to figure out the new link_to in Rails 3 but i still don't get it
In Rails 2 I do:
<%= link_to_remote "My Link",:url=>{:action=>:myaction},:with=>"'data='+$('#someField').attr('value')" %>
but with the new syntax in Rails 3 how should it be?
I'm trying something like
<%=link_to "My Link",{:action=>"myaction"},:with=>"'data='+$('#someField').attr('value');",:remote=>true%>
but I'm not getting the quantity params in the controller's action
Something like this will send a "data" parameter with the value = 10.
link_to "My Link", { :controller => 'myctrler', :action=>"myact", :data=> 10 }, :remote=>true
I've never seen/used the :with option before. I'm sorry I can't help on that one.
They say its no longer supported in this answer on How to send Javascript Variable to a controller's action with the link_to helper?
you just need to add the variable with the value in the path url, for example:
<%= link_to "SEND DATA", "server_function?myData=10", remote: true %>
and if you need to send more than one parameter you must to use & for example ?myData=10&myOtherData=12, where the params are myData with the value of 10 and myOtherData with 12.