views:

295

answers:

5

The form_remote_tag seems to generate the correct html, but format.html is rendered instead of format.js. The two most common answers to format.html being rendered instead of format.js are renaming the file *.rjs to *.js.rjs and making sure the the javascript files are actually loaded. Both of these have been done. Let me know if there's any other information I can provide.

[edit] create.js.rjs has been added below. [/edit]

Rails 2.3.2

view/projects/show.html.erb

<%= form_remote_tag :url => { :controller => :milestones, :action => :create }
    #:update => 'milestone-list',
    #:url => { :controller => "milestones", :action => :ajax_create},
    #:position => 'bottom'
%>

<%= render :partial => 'milestones/form' %>
<input type="hidden" id="milestone_project_id" name="milestone[project_id]" value="<%[email protected] %>" />

<%= submit_tag 'Add Milestone' %>

milestones_controller.rb

def create
 @milestone = Milestone.new(params[:milestone])

 respond_to do |format|
    if @milestone.save
        flash[:notice] = 'Milestone was successfully created.'
        format.js   {render :layout => false }
        format.html { render :show }#redirect_to(@milestone) }
        format.xml  { render :xml => @milestone, :status => :created, :location => @milestone }
    else
        format.html { render :action => "new" }
        format.xml  { render :xml => @milestone.errors, :status => :unprocessable_entity }
    end
 end
end

headers

(domain names and ips have been changed. The server is mongrel with apache setup as proxy)

*  ACCEPT: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
* HOST: 127.0.0.1:8004
* X_FORWARDED_HOST: slate.project.mydevserver.com
* VIA: 1.1 project.mydevserver.com
* USER_AGENT: Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6
* ACCEPT_LANGUAGE: en-us,en;q=0.5
* REFERER: http://slate.project.mydevserver.com/projects/2
* VERSION: HTTP/1.1
* X_FORWARDED_SERVER: project.mydevserver.com
* X_FORWARDED_FOR: 1.2.3.4
* ACCEPT_CHARSET: ISO-8859-1,utf-8;q=0.7,*;q=0.7
* CONNECTION: Keep-Alive
* ACCEPT_ENCODING: gzip,deflate

generated html

<form action="/milestones" method="post" onsubmit="new Ajax.Request('/milestones', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;">
<div style="margin:0;padding:0"><input name="authenticity_token" type="hidden" value="wTS9QPqFiGvP3RVlvKsA49Z6xtEGH9gmcLP//XC0whc=" /></div>

Title: <input id="milestone_title" name="milestone[title]" size="30" type="text" /><br />
Description: <input id="milestone_description" name="milestone[description]" size="30" type="text" /><br />
Days: <input id="milestone_days" name="milestone[days]" size="30" type="text" /><br />

<input type="hidden" id="milestone_project_id" name="milestone[project_id]" value="2" />

<input name="commit" type="submit" value="Add Milestone" />
</form>

create.js.rjs

page.insert_html :bottom, 'milestone_list', "Hi"
A: 

Any chance you've got Javascript disabled in the browser you're testing from? That would explain your results.

EmFi
How do I say no in over 15 characters.
Elizabeth Buckwalter
I didn't expect it to be the right answer, but it was the only thing I could come up with that would describe your problems. Everything else looks right.
EmFi
No problem. It's the obligatory "is it plugged in" question. That's what has me banging my head against the wall. I really hope that it's an easy answer! Maybe it's an apache proxy to mongrel issue. Hmmm.
Elizabeth Buckwalter
What kind of request does Firebug or similar tool say you're performing?
EmFi
The request is above.
Elizabeth Buckwalter
What's the full URL of the request? I see your referrer header, but maybe it has to do with your proxy, as you mentioned. Is it possible that you are making requests across sub-domains, or something along those lines? Unfortunately, I don't know enough about Apache configurations or reading into all the request headers, so I'm tapped other than that. Your code above works perfectly for me (in 2.3.5, mind you).
theIV
Thanks for testing it for me! I'll check the proxy configuration and see if it's not passing the request headers properly.
Elizabeth Buckwalter
A: 

Have you tried removing the {render :layout => false } block from format.js and just doing something like

...
...
format.js
format.html { render :show }#redirect_to(@milestone) }
...

To my understanding RJS does not deal with layouts as it does not yield within them, I could be wrong about that though. But I personally have never actually used a :layout option on my js responses before.

Also, your render call is missing "what" to render. Like...

render :js => "alert('this is javascript')"
render :action => "some_action"
render :xml => ....
nowk
Yes I have tried removing the block. It's not missing what to render. It's supposed to run create.js.rjs, and just show it on the original page. The problem is that it's doing format.html instead of format.js, because the accept headers are requesting text/html instead of text/javascript
Elizabeth Buckwalter
Have you tried running the app just on Mongrel (maybe locally) alone with out the Apache + Mongrel mix? Your code looks pretty kosher, so perhaps something is changing the headers as the requests go through your Apache + Mongrel setup. Also, I'm sure you probably tried this. But if your not in "development" are you restarting the app after each deployment/change?
nowk
It's on a linux box with nno xwindows. I haven't heard of a javascript enabled text browser. Unfortunately, the bounty ends in an hour, so I won't be able to set Mongrel up in time to point out.
Elizabeth Buckwalter
A: 

You can easily check if your code is fine by forcing the format to be javascript with adding :format => :js to the url hash. It will explicitly add the format to the url and thus force the javascript to be rendered, but it will break the graceful degrading for javascript-disabled browser. If you want to keep that feature - I believe you should look into your proxy (apache) config, as it was said above - apparently it's messing with your headers.

Try to capture the request when it's send by your browser (with Firebug or LiveHTTPHeaders for Firefox) and compare it with the one you see on the server side, I believe you'll see that there lies your problem.

morhekil
Adding that to the url gave me an error of "cannot find create.js.erb" I copied create.js.rjs to create.js.erb and, as expected, it simply shows the contents of the file instead of running it.
Elizabeth Buckwalter
The headers are the same between what I print to the html page that responds to the request and LiveHTTPHeaders. I have a feeling there might be some tiny implemented change that's not in the Rails documentation.
Elizabeth Buckwalter
I'm giving you the bounty, because you're the only one who addressed the header issue. It's still not fixed, but at least I can ask a different question here. (When using :format => :js, why does rails look for create.js.erb instead of create.js.rjs?) Thanks! Look for it tomorrow.
Elizabeth Buckwalter
when you're forcing the format of your response, you should disable the automatic mime type guessing based on accept headers, or your forced format won't work properly. It seems that for some reason you have accept headers processing enabled, though it should be disabled by default since rails 2.2. Use config.action_controller.use_accept_header = false to do that - see more details here http://guides.rubyonrails.org/2_2_release_notes.html#other-action-controller-changes
morhekil
A: 

One idea is that you can rewrite your create action in your controller to include a test to see if the request is a XHR (this is detailed further on in the docs), here's a simplified version:

def create

  @milestone = Milestone.new(params[:milestone])

  if @milestone.save
    if request.xhr?
      render :update do |page|
        page.insert_html :bottom, 'milestone_list', "Hi"
      end
    else
      respond_to do |format|
        flash[:notice] = "Milestone saved"
        format.html
      end
    end
  else
    respond_to do |format|
      flash[:error] = "There was a problem saving the milestone"
      format.html { render :action => :new }
    end
  end

end

That could stand some DRYing up, but you should get the idea.

Dan McNevin
request.xhr completely failed. Which is the same problem. In this case TIMTOWDI should be renamed to TIMTOWTGTSE (there is more than one way to get the same error). :D
Elizabeth Buckwalter
A: 

If you run rake:routes, does the route you're using end in (.:format) ?

JRL
Yes. (15 chars)
Elizabeth Buckwalter