我有一個電子郵件表格。但是,我正在製作一個測試電子郵件表單,用戶可以添加一封唯一的電子郵件,並通過電子郵件測試將其發送到該特定的電子郵件。如何將AJAX的vars從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
}
我道歉提前如果這是一個愚蠢的新手問題。 :(
test_email_admin_email_blast_path是否接受GET請求? (嘗試使用「rake routes | grep test_email_admin_email_blast」來查看Rails如何解釋您的routes.rb) – tobym 2010-09-15 18:41:59
是的。 test_email_admin_email_blast GET /admin/email_blast/test_email(.:format){:controller =>「admin/email_blasts」,:action =>「test_email」} – Trip 2010-09-15 19:05:36