如何将AJAX变量从jQuery传递给控制器​​?

前端之家收集整理的这篇文章主要介绍了如何将AJAX变量从jQuery传递给控制器​​?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个电子邮件表格.但我正在制作测试电子邮件表单,用户可以在其中添加唯一的电子邮件,并将电子邮件测试发送到该特定电子邮件.

为了方便起见,我决定通过ajax执行测试电子邮件,并将整个内容粘贴到另一个电子邮件表单中.

我无法弄清楚如何将变量从我的HAML发送到我的控制器

new.html.haml

- form_tag admin_email_blast_path do
  Subject
  %br
  = text_field_tag 'subject',:class => "mass_email_subject"
  %br
  Body
  %br
  = text_area_tag 'message','',:name => 'editor1',:class => "mass_email_body"
  %br/
  = submit_tag 'Send Email',:class => 'button'

  .grid_3.right.align_right.suffix_1  # <= TEST EMAIL :D
    = text_field_tag 'email',nil,:style => "width: 150px;",:class => "test_email_address"
    = link_to 'Test Email',test_email_admin_email_blast_path,:class => 'button test_email'

JS

$(".test_email").live("click",function() {

  var email = $(".test_email_address")
  var subject = $(".mass_email_subject");
  var body = $(".mass_email_body");

  data = "email=" + email.val() + "&subject" + subject.val() + "&body" + body.val();

  $.ajax({type: "GET",url: $(this).attr("href"),dataType: "script"
          data: data
          });
  return false;
});

调节器

def test_email
  debugger
  email = params[:email]
  subject = params[:subject]
  body = params[:body]
  Notifier.deliver_email_blast(email,subject,body)
end

的routes.rb

admin.resource :email_blast,:member => {
                                :test_email => :get
                                }

如果这是一个愚蠢的新手问题,我会提前道歉. 原文链接:https://www.f2er.com/jquery/428497.html

猜你在找的jQuery相关文章