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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to