rails params.merge in link_to - ruby-on-rails

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 :)

Related

link_to using PUT method syntax to a different controller in rails 3

I try to call an action of my Gallery controller from a Portfolio view. A Portfolio is made of many galleries.
I try this:
<%= link_to("Heart", gallery_path(gallery), :action => "like", :method => :put , :remote => true) %>
<%= link_to("Heart", :controller => :galleries, :action => "like", :method => :put , :remote => true) %>
And I obtain:
Heart
and
Heart
I want to get but i m stuck...:
Heart
Any RAILS God to help me ?
I believe you're getting behaviour because you're trying to use the "path helper" and "params hash" styles in the same link_to (see the docs for more details). I prefer the path helper style, so I'd write the link like this:
<%= link_to(
'Heart',
like_gallery_path(gallery),
{:method => :put, :remote => true}
) %>
If you like the params hash style, you'd write:
<%= link_to(
'Heart',
{:controller => 'galleries', :action => 'like', :id => gallery.id},
{:method => :put, :remote => true}
) %>
Note that the URL parameters (controller, action, etc.) are in a separate hash from the link parameters (method & remote).
Hope that helps!
Try with:
<%= link_to "Heart", gallery_path(gallery), :url => { :controller => "galleries", :action => "like"}, :method => :put, :remote => true) %>

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

Turn a button to a link with the same exact functionality

So I have this button:
<%= button_to "+1", video_votes_path( :video_id => video.id, :type => "up" ), :remote => true %>
It calls the create method in the video_votes controller, and also sets params[:type]. I want to turn this button into a link so that is does exactly the same thing. How do I do this?
button_to "+1", video_votes_path( :video_id => video.id, :type => "up" ), :remote => true
=>
link_to "+1", video_votes_path( video, :type => "up"), :remote => true, :method => :post
Change button_to to link_to_remote with an extra parameter: :method => :post, and take out the remote.
Uh, leave out the method if you want a GET.

RoR remote_function pass class name of element

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

Ruby on Rails: link_to_remote and rel

I want a link to remote to have a rel tag, because I want to use facebox with it.
I had it working with a regular link to... but I needed the link to remote to handle the event that a user doesn't have javascript enabled.
this, currently does't work (except for the non-javascript part )
<%= link_to_remote "Ask a Question",
{:url =>
{:action => :ask_question,
:id => #container.id.to_s,
:javascript_disabled => false
}, :rel => 'facebox'},
:href => url_for(
:controller => :view,
:action => :ask_question,
:id => #container.id.to_s,
:javascript_disabled => true) %>
In link_to_remote you pass in HTML options (like rel) in the third argument. In your code you're passing it in the second (i.e. the first hash). Try this instead:
<%= link_to_remote("Ask a Question",
{ :url => { :action => :ask_question,
:id => #container.id.to_s,
:javascript_disabled => false
}
},
{ :href => url_for( :controller => :view,
:action => :ask_question,
:id => #container.id.to_s,
:javascript_disabled => true ),
:rel => 'facebox'
}
)
%>
(As you know, some of the parentheses and curly braces are optional, but here I've included all of them for clarity, and probably would keep them in since you're passing a lot of complex arguments here.)

Resources