如何将此link_to_function转换为rails 3中的link_to

时间:2021-09-14 20:45:14

I have this link_to_function

我有这个link_to_function

  = link_to_remote 'Populate Info From ID', :url => {:controller => 'something', 
  :action => 'populate_from_id'}, 
  :with => "'id=' + $('account_id').value + '&field_prefix=purchaser'", 
  :update => {:failure => 'account_id_error'}

I have converted many of them in a rails upgrade with , :remote => true, :method => :post

我已经使用,:remote => true,:method =>:post在rails升级中转换了许多

But i dont know how to add the with condition to grab the value out...any ideas

但我不知道如何添加条件来抓住价值......任何想法

2 个解决方案

#1


1  

All the AJAX-specific options representing callbacks are gone in Rails 3 link_to helpers. You'll have to write your own javascript to handle more complex remote actions like your example.

表示回调的所有特定于AJAX的选项都在Rails 3 link_to帮助程序中消失了。您必须编写自己的javascript来处理更复杂的远程操作,例如您的示例。

Here's a quick rewrite:

这是一个快速重写:

# Your template

= link_to 'Populate Info From ID', :url => {:controller => 'something', 
  :action => 'populate_from_id'}, :id => "#populate_info"


# In javascript, assuming jquery and 
# an element #account_id with a data-id attribute
$("#populate_info").on("click", function() {
  $.ajax({
    method: "POST",
    data: { id: $('#account_id').data("id"), field_prefix: "purchaser" }
    error: account_id_error
  });
  return false;
});

Useful blog post: http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/ Lots of great documentation here: http://api.jquery.com/jQuery.ajax/

有用的博客文章:http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/这里有很多很棒的文档:http://api.jquery.com/jQuery.ajax/

#2


0  

You beat me to it I came up with this

你打败了我,我想出了这个

$('.populate_id').click(function(e){
  e.preventDefault();
  var url = $(this).attr('href'); 
  var failure = $('#' + $(this).attr('failure'));
  failure.html('');
  var element = $('#' + $(this).attr('element'));
  var id = element.val();
  var url_extra = 'account_id=' + id + '&field_prefix=purchaser';
  $.ajax({
      url: url,
      data: url_extra,
      type: 'post',
      error: function (data) {
        failure.html(data.responseText);
      }
    });
  return false;   
});

#1


1  

All the AJAX-specific options representing callbacks are gone in Rails 3 link_to helpers. You'll have to write your own javascript to handle more complex remote actions like your example.

表示回调的所有特定于AJAX的选项都在Rails 3 link_to帮助程序中消失了。您必须编写自己的javascript来处理更复杂的远程操作,例如您的示例。

Here's a quick rewrite:

这是一个快速重写:

# Your template

= link_to 'Populate Info From ID', :url => {:controller => 'something', 
  :action => 'populate_from_id'}, :id => "#populate_info"


# In javascript, assuming jquery and 
# an element #account_id with a data-id attribute
$("#populate_info").on("click", function() {
  $.ajax({
    method: "POST",
    data: { id: $('#account_id').data("id"), field_prefix: "purchaser" }
    error: account_id_error
  });
  return false;
});

Useful blog post: http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/ Lots of great documentation here: http://api.jquery.com/jQuery.ajax/

有用的博客文章:http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/这里有很多很棒的文档:http://api.jquery.com/jQuery.ajax/

#2


0  

You beat me to it I came up with this

你打败了我,我想出了这个

$('.populate_id').click(function(e){
  e.preventDefault();
  var url = $(this).attr('href'); 
  var failure = $('#' + $(this).attr('failure'));
  failure.html('');
  var element = $('#' + $(this).attr('element'));
  var id = element.val();
  var url_extra = 'account_id=' + id + '&field_prefix=purchaser';
  $.ajax({
      url: url,
      data: url_extra,
      type: 'post',
      error: function (data) {
        failure.html(data.responseText);
      }
    });
  return false;   
});