Hi all,

I'm submitting a form using jQuery, but in the respond_to block only the
format.html action is called.

This is the javascrip:

send_button.on('click', function(){
var form = $(this).closest('.modal').find('form');

$.post('/messages', form.serialize(),
function(data, status, xhr){
console.log(data);
});
);
});

However, when this hits the controller, 'console.log(data)' outputs html
(either root_url or the validation array).

The code in the controller looks like:

respond_to do |format|
if @m.save
format.html {redirect_to root_url, :notice=>"Message sent"}
format.json {render :json=>@m, :status=>:created, :location=>@m}
else
format.html {render :action=>:new}
format.json {render :json=>@m.errors,
:status=>:unprocessable_entity}
end
end

Obviously, I'm not using link_to_function or anything, so I'm wondering
if the request format header isn't being set properly without using the
built-in rails functions?

Any help would be appreciated.

Many thanks.

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Valery Kvon at Feb 21, 2012 at 9:08 am

    On 20.02.2012, at 22:57, Oliver Fox wrote:

    Hi all,

    I'm submitting a form using jQuery, but in the respond_to block only the
    format.html action is called.

    This is the javascrip:

    send_button.on('click', function(){
    var form = $(this).closest('.modal').find('form');

    $.post('/messages', form.serialize(),
    function(data, status, xhr){
    You call pure Javascript request.
    console.log(data);
    });
    );
    });

    However, when this hits the controller, 'console.log(data)' outputs html
    (either root_url or the validation array).

    The code in the controller looks like:

    respond_to do |format|
    if @m.save
    format.html {redirect_to root_url, :notice=>"Message sent"}
    format.json {render :json=>@m, :status=>:created, :location=>@m}
    format.json - is NOT javascript hook,

    use format.js

    or old approach:
    if request.xhr?
    do action
    end


    else
    format.html {render :action=>:new}
    format.json {render :json=>@m.errors,
    :status=>:unprocessable_entity}
    end
    end
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedFeb 20, '12 at 6:57p
activeFeb 21, '12 at 9:08a
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Valery Kvon: 1 post Oliver Fox: 1 post

People

Translate

site design / logo © 2021 Grokbase