如何使用通過ajax發送的JSON數據到rails操作

[英]how to use the JSON data sent via ajax to a rails action


I basically have a form whose data are to be sent to a controller via post using ajax. I think I am sending it right but I'm unable to use it in my controller. I'm getting an error like:

我基本上有一個表單,其數據將通過post使用ajax發送到控制器。我想我正在發送它但我無法在我的控制器中使用它。我收到的錯誤如下:

Error: wrong number of arguments (given 1, expected 0)

錯誤:參數數量錯誤(給定1,預期為0)

MY CODE:

我的代碼:

Submitting the form using ajax request:

使用ajax請求提交表單:

 $('#compose-form').submit(function() {  
var valuesToSubmit = $(this).serialize();
$.ajax({
    type: "POST",
    url: $(this).attr('action'), 
    data: valuesToSubmit,
    dataType: "JSON" 
}).success(function(json){
    $.each(json, function (key, data) {
        var display;
        if(data){
             display = "Mail delivered sucessfully!"
        } 
        else{
            display = "Mail can't be delivered right now, please try again later!"
        }
        $.notify({
              icon: 'pe-7s-science',
              message: display
        },{
              type: 'info',
              delay: 0,
              placement:{
                from: 'bottom',
                align: 'right'
        }
        });
        });
});
return false; // prevents normal behaviour
});

The action in my controller

我控制器中的動作

def send

to = params[:to]
cc = params[:cc]
sub = params[:sub]
body = params[:body]

@mail = view_context.sendMail(to, cc, subj, body)

if @mail.deliver!
@wapas = true 
else
@wapas = false
end

redirect_to 'mail/inbox' if(@wapas)
respond_to do |format|
format.json { render json: @wapas.to_json }
end

end

I have also defined the post path in my routes.rb

我還在routes.rb中定義了post路徑

Thanks in advance!

提前致謝!

1 个解决方案

#1


2  

Don't use a method called send - ruby itself has a very core method by this name: Object#send which you're overriding and then other internal things are trying to call it and failing.

不要使用名為send的方法 - ruby​​本身有一個非常核心的方法,就是這個名字:對象#發送你要覆蓋的,然后其他內部的東西試圖調用它並失敗。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/04/14/e114e22334cc76495bf99ab9f369fea8.html



 
  © 2014-2022 ITdaan.com 联系我们: