RoR remote_function pass class name of element - ruby-on-rails

In remote_function how can i pass the className of the form element
i have the following code,
<%=link_to(content_tag('span',t('country.name')),'#',
{:title=>t('country.name'),**:class=>"change_list"**,
:onclick=>remote_function(:url => {:action => :change_view},
:with =>"'**change**=**'+ ?????"**)}) %>
so, on onclick, i want to pass the parameter :change with the value of :class=>"change_list"
thanks,

If I am understanding correctly, that you want to pass the class name of the anchor tag in your Ajax request, then the following should work (re-formatted to try and improve readability):
<%= link_to(
content_tag('span', t('country.name')),
'#',
{ :title => t('country.name'),
:class => "change_list",
:onclick => remote_function(
:url => {:action => :change_view},
:with => "'change=' + this.className"
)
})
%>

Related

rails params.merge in link_to

I am trying to add a URL parameter in a link_to block.
The code currently <%= link_to "Submit", :action => 'renderChart', :class => "btn", :remote => true, :params => params.merge(:limit => 5) %>
but this gives me an error.
It adds the :class and :action into the url parameter, not just the :limit. Why?
EDIT:
I add other URL params from another link that looks like this
link_to "Toggle Sort Direction",:action => 'renderChart', :remote => true, :params => {:sort => "desc"}
so when the user clicks the other link I want to add the limit to the url params and keep the sort params
Use this
<%= link_to "Submit",{ :action => 'renderChart', :remote => true, :limit => 5, :sort => "desc"}, :class => "btn" %>
Separate out the html_options: class is an html_option so pass it last.
Refer to link_to documentation.
UPDATE
As per the OP's concern in EDIT section of Question:
I add other URL params from another link that looks like this
link_to "Toggle Sort Direction",:action => 'renderChart', :remote => true, :params => {:sort => "desc"}
params :sort => "desc" are for Toggle Sort Direction link and they cannot be connected to the Submit link. When you click on a particular link, params specified in the link would be added to the params hash. So, if you need to pass :sort => "desc" as params upon clicking on Submit link then specify them explicitly as shown in my answer above.
I finally managed to get a solution myself.
If I very simply do this: :params => {:limit => ..., :sort => params[:sort]} i get exactly what I need. If there is a sort param it keeps it the way it is.
You need to explicitly separate the hashes:
<%= link_to "Submit", { :action => 'renderChart', :class => "btn", :remote => true }, params.merge(:limit => 5) %>
Take the link_to out and you have an implicit hash (key-value pairs) and Ruby is smart enough to know you want a hash:
:action => 'renderChart', :class => "btn", :remote => true, params.merge(:limit => 5)
But that last thing - that's not a key-value pair - it's a hash. So really, you have this:
{ :action => 'renderChart', :class => "btn", :remote => true, { ... } }
If you take Rails out of the mix:
{ x: 'value', {} }
And that's simply not a valid Hash :)

Rails link_to_if Class Doesnt Display Correctly

For the life of me, i cant figure out why this problem is happening. I used the link_to helper all the time, but iv only used the link_to_if helper a few times, and this time I cant get the link to take a CSS class.
Here's my link
<%= link_to_if(step.sequence > 1, raw('<i class="icon-chevron-up"></i>'), url_for(:controller => :test_steps, :action => :update_sequence, :direction => 'up', :id => step.id, :test_script_id => #test_script), { :class => 'btn btn-mini' })%>
The image displays, with no link as expected, but the CSS class defined at the end does not, instead it just has no class. This is the same format I use in all my link_to helpers.
Can anyone explain why?
Using link_to_if, only the name --in your case the result of raw('<i class="icon-chevron-up"></i>')-- will be returned if the condition fails. All options that would otherwise apply to the link tag will be ignored.
See the source.
Try passing an empty hash as the options argument, so that { :class => '...' } is assigned to the html_options argument. Untested
<%= link_to_if(step.sequence > 1, raw('<i class="icon-chevron-up"></i>'), url_for(:controller => :test_steps, :action => :update_sequence, :direction => 'up', :id => step.id, :test_script_id => #test_script), {}, { :class => 'btn btn-mini' })%>
Wrap the link_to_if or link_to_unless in a span:
%span.pull-right
= link_to_unless Foo.deleted.empty?, "<i class='icon-white icon-trash'></i> Undelete Foo".html_safe, deleted_foos_path
Above code sets a css class (pull-right) on whatever is displayed - full link or just its text.

how to set class attribute with form_tag in rails

I have the following line of haml:
=form_tag :action => 'create', :controller => 'comments', :class => 'comment_form' do
But the html that gets output is:
<form accept-charset="UTF-8" action="/comments?class=comment_form" method="post"></form>
I want to set the class. How do I do this?
<-- Update -->
With this:
=form_tag ({ :action => 'create', :controller => 'comments' }, { :class => 'comment_form' }) do
I get this error:
syntax error, unexpected ',', expecting ')'
...', :controller => 'comments' }, { :class => 'comment_form' }...
<-- Second Update -->
The problem above is the space between 'form_tag' and '(' #woahdae's answer is correct
form_tag takes 2 options hashes, the first being passed to url_for, the second being passed to the form builder.
So, you have to do it like:
= form_tag({:action => 'create',...}, {:class => 'comment_form'}) do
otherwise Rails thinks all the key/value pairs are for url_for, which will append any keys it doesn't understand as query parameters.
This works for me:
form_tag named_route, :method => :put, :class => 'disable_on_submit'
With Rails 3.0.15
On Rails 5, you can do the following:
<%= form_tag(your_named_path, {class: 'form-inline'}) do %>
<% end %>
You can do follow as:
form_tag your_path, method: :get, id: "your_id", class: "your_class" do
end
In case you found this question and actually wanted to solve class naming for a form_for:
<%= form_for #task, html: {:class => "custom_class"} do |f| %>

How can I pass variable with Ajax link in Rails?

I want to pass variable with link_to_remote in Rails 2.3. Following is my code for passing variable. But controller did not get that variable. Anybody can help me ?
<%= link_to_remote 'Add new event', :url => {:controller => 'events', :action => 'new' }, :with=> 'event' %><br>
Any hash key you add that's not part of the keyword set (controller, action, format) will be appended to your URL as an argument.
ie.
<%= link_to_remote "Add New Event", :url => {:controller => 'events', :action => 'new', :var => 'event'} %>
Would yield
/events/new?var=event
Hope this would solve your problem,I have not tried.
<%= link_to_remote "Add New event",
:url => :action => "list",
:with => " 'name=' +$('div-id-of-name-text-box').value + '&city=' +$('div-id-of-city-text-box').value + '&country=' +$('div-id-of-country-text-box').value " %>

How to add tooltip with link_to_remote image_tag in ruby on rails?

i want show tooltip with image tag and i have written code like below but i am not able to get tooltip while mouseover...
Code:
<%= link_to_remote image_tag('../images/save_active.gif',:method => "post",:tooltip=>"Save", :border => 0), :url => {:controller => "ptcgeodatabase" } %>
You can try this, if you are not using javascript tooltip
<%= link_to image_tag('something.png'), '#', :title => "Save" %>
Unless you're using a javascript library to generate tooltips specially, you need to use the title attribute on your a element (I've split this onto multiple lines for readability):
<%= link_to_remote image_tag('../images/save_active.gif',
:method => "post",
:border => 0),
:url => { :controller => "ptcgeodatabase" },
:html => { :title => "Save" } %>

Resources