daredevil11
daredevil11

Reputation: 107

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:

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

MY CODE:

Submitting the form using ajax request:

 $('#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

Thanks in advance!

Upvotes: 1

Views: 152

Answers (1)

smathy
smathy

Reputation: 27971

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.

Upvotes: 2

Related Questions