I know this is a stupid questions and have very similar ones out there but none of them really solve my problem:
#application.js
//= require jquery
//= require jquery_ujs
//= require_tree .
#form
<%= form_for @comments, :remote => true, do |comment| %>
.....
<% end %>
#controller
def create
@comments = Comment.new params[:posts]
if @posts.save
respond_to do |format|
format.html {render 'something'}
format.js {render :content_type => 'text/javascript', :layout => false}
end
end
#app/view/comment/create.js.erb
alert("hello world, please work!");
With the code, I just want to add the comments without refreshing the page, and the posts are appended to the page. Anyhow, whenever I submit a post, it always goes to the “respond_to” format.html line, which i have no idea why. When I look at the generated form, it reads fine with data-remote sets correctly:
<form accept-charset="UTF-8" action="...." data-remote="true" id="..." method="post"
When I look at the HTTP accept it is
HTTP_ACCEPT: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" even I set the content_type to be text/javascript.
I found one say that by adding charset :content_type => ‘text/javascript;charset=UTF-8′ would help, but it does no magic.
I also try adding this onto the application.js but also no magic
$ (function() {
$ .ajaxSetup({
'beforeSend': function(xhr) {
xhr.setRequestHeader("Accept", "text/javascript");
}
});
});
Can anyone guide me on this? And btw, the errors in my Webbrick server shows “406 unacceptable error something”. I am really frustrated on this. What I suspect is something about the content type that I am doing it wrongly…