Turn a button to a link with the same exact functionality - ruby-on-rails

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.

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

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

Rails / Haml: How to create a post form?

I'm trying to make a simple form, but it's working not so fine.
This is my current form code:
%form{ :controller => 'tool', :action => 'activation', :method => 'post' }
%table{ :border => 0, :width => "100%", :height => "100%" }
%tr{ :align => "center", :valign => "center" }
%td
%input{ :type => "text", :name => "accountName" }
%input{ :type => "submit", :name => "submit", :value => "login" }
I am getting this url when trying to send data via form: 10.0.0.2:3000/activation.
I know that I can make route tool#activation to activation, but it's a wrong way, I want to sent post query to 10.0.0.2:3000/tool/activation, but :action => 'tool/activation' also is a bad way as far as I understand.
Can you give me advice ?
You should use the rails helper tags.
= form_tag tool_activation_path, :method => :post do
# The table
# The row
# The data
= text_field_tag "accountName", ""
= submit_tag "Submit"
See more here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html
Also, you should try to avoid unnecessary tables to style your layout. Instead, look to using CSS.

In my rails erb, how can i replace an ajax button_to by a link_to?

I have the following button :
<%= button_to 'Add to Cart', line_items_path(:product_id => product),
:remote => true %>
I want to replace it by a link_to containing an image with text on it.
I am ok with the HTML CSS part, but i want the request to be for line_items#create not for line_items#index
How can i do that?
Try this:
<%= link_to "Add to Cart", {:controller => "line_items", :action => :create}, :remote => true %>
And don't forget to update routes.rb too, e.g.:
get "/blablabla", :to => "line_items#create"
After a bit of try and error i found that :
<%= link_to ("<div>Ajouter au panier</div>"+image_tag("some.jpg")).html_safe,
line_items_path(:product_id => #product),
:action => :create,
:remote => true,:method => :post%>
It works perfectly fine!

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