Questions in getting AJAX to work (received "406 - Not Acceptable" )

334 views
Skip to first unread message

chandramouli s

unread,
Jul 29, 2011, 1:59:48 AM7/29/11
to bangalorerug
Hi,

In a single controller

I have an "add" link on the index page (index.html.haml)

= link_to "Add new product", new_product_path, html_options = {:remote => true}

Since I have a :remote => true, I assume it (the browser) expects an
Ajaxy reply - Json or XML.
(It would be nice if someone can clarify this, can the reply be HTML
or XML that is expected to be handled by js event handlers or does it
have to be a JS response)

On the controller side I have

def new
@product = Product.new
respond_to do |format|
format.json
end
end

and the corresponding view template "new.js.erb"

$('#content').replace("<%= escape_javascript(render(:partial =>
'productform', :object => @product))%>");


However when I click on the link I get a "406 - Not Acceptable", and
nothing on the browser window.

--
Thank you
Chandramouli

chandramouli s

unread,
Jul 29, 2011, 2:15:43 AM7/29/11
to bangalorerug
Also it tries to process it as html

Started GET "/products/new" for 127.0.0.1 at 2011-07-29 11:41:36 +0530
Processing by ProductsController#new as HTML
Completed 406 Not Acceptable


--
Thank you
Chandramouli

hemant

unread,
Jul 29, 2011, 3:01:22 AM7/29/11
to bangal...@googlegroups.com
There were some bad versions of jquery-rails plugins floating around,
which weren't setting content-type properly afaik.

Ensure, you are running latest version of jquery-rails and jquery.

Still, why are you making a 'js' request, when you are returning json
response? If you intend to make 'json' request, you may have to set
Content-Type manually or write custom JS than relying on :remote =>
true.

--
Let them talk of their oriental summer climes of everlasting
conservatories; give me the privilege of making my own summer with my
own coals.

http://gnufied.org
http://twitter.com/gnufied

chandramouli s

unread,
Jul 29, 2011, 3:58:24 AM7/29/11
to bangal...@googlegroups.com
Hi Hemant,

I tried updating jquery-rails to the latest version (1.0.12),
regenerated the js files. But this does not seem to help.

Initially I was sending a Js response, but I just wanted to see if I
would get anything different with a Json one.


--
Thank you
Chandramouli

hemant

unread,
Jul 29, 2011, 4:07:11 AM7/29/11
to bangal...@googlegroups.com
Well,

First, you should stop using format.json since you are rendering
*.js.erb file. But still, you have a problem, which is - your request
is coming as 'HTML' rather than 'JS'.


My best guess is something screwed with rails.js or something else is
sitting in between your request and screwing up the content-type.
Beyond that, sorry - I don't think I can help. :(

chandramouli s

unread,
Jul 29, 2011, 4:20:02 AM7/29/11
to bangal...@googlegroups.com
Thank you Hemant, at least now I have some direction of where I might
have a problem. But by content-type do you mean the one in the js
request?


--
Thank you
Chandramouli

srijith c

unread,
Aug 2, 2011, 12:38:01 AM8/2/11
to bangal...@googlegroups.com
Hi,
Try this...


def new
   @product = Product.new
   render :update do |page|
     page.replace_html '"content", :partial => 'productform'
   end
end

Remove new.js.erb file.

Thanks
   Srijith C

hemant

unread,
Aug 3, 2011, 5:07:16 AM8/3/11
to bangal...@googlegroups.com
Hi,

On Tue, Aug 2, 2011 at 10:08 AM, srijith c <bab...@gmail.com> wrote:
> Hi,
> Try this...
>
> def new
>    @product = Product.new
>    render :update do |page|
>      page.replace_html '"content", :partial => 'productform'
>    end
> end
>
> Remove new.js.erb file.
>

I don't think that is advisable.RJS has been dead in the water and
unless you are using prototype helpers to generate Javascript from
Ruby, above code won't work.

srijith c

unread,
Aug 3, 2011, 5:16:07 AM8/3/11
to bangal...@googlegroups.com
Yes...

RJS been extracted from RAILS 3 and by make use of "prototype-rails" gem we can achieve this.

gem 'prototype-rails', :git => 'git://github.com/rails/prototype-rails.git'

Thanks
   Srijith C

hemant

unread,
Aug 3, 2011, 5:24:15 AM8/3/11
to bangal...@googlegroups.com
Well, couple of things:

1. In Rails 2, if there was no Content-Type match for the request, the
default respond_to block gets executed. But in Rails 3, if there is no
Content-Type match, then Rails just throws 406 - Not Acceptable. So,
in other words, using RJS won't solve his problem. Having said that,
if it works, I will be surprised and it won't be first time, Rails has
surprised me.

2. There was a reason RJS was dropped in favour of using plain
Javascript. So, unless you are telling, "to hell with that" and I will
keep using RJS anyways, thats your call which I respect. But
suggesting thats as a practice to others, without looking deeply into
the actual problem itself is not good and I condone that.

Punit Rathore

unread,
Aug 3, 2011, 5:28:34 AM8/3/11
to bangal...@googlegroups.com
+1 Unobtrusive javascript is a cleaner way to express your javascript views. RJS makes your controller code hard to read and test.

Cheers
Punit Rathore

Reply all
Reply to author
Forward
0 new messages