Arvind Rajput Arvind Rajput - 1 year ago 135
Ruby Question

link_to_remote equivalent in rails 4

I am upgrading from rails2.3.5 to rails4 and i am facing difficulty to convert link_to_remote into rails 4

link_to_remote("Retire", :url => { :action => :delete, :id => },
:condition => "confirm_retire(#{})", :html => {:style => 'color: #CC0000;float:right;margin-right:8px;'})

I know that we need for this :remote => true with link_to like

link_to('Retire', {:action => :delete, :id =>},
:condition => "confirm_retire(#{})", :remote => true,
:class => 'retire')

But i am facing problem with :condition => "confirm_retire(#{})",

how to handle this ?, i did lot of research but not found any solution

Answer Source

The condition option isn't supported by link_to. You could perform that check in a js file using a data attribute on the link element to provide a value for the condition.

link_to('Retire', {:action => :delete, :id =>},:remote => true, :class => 'retire', :data-user-retire => "your_condition")

$('.retire').on('click', function(){
  if ($(this).attr('data-user-retire') == "your condition") {
    return true;
  } else {
    $(this).append("some message")
    return false;

This is just an example, you'll need to amend it for your requirements.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download