Awesome, thanks for the continued help. I tried the first thing you
suggested, putting in the default AJAX settings. Being an
inexperienced Rails programmer, I really don't know what you mean by
number 2. How do I sort my rails headers? I'm fairly sure I'm
running Rails 2.2.2
A few more things that I tried: setting the 'accepts' hash to be
{ script: "text/javascript" }, and the 'dataType' string to be
"script". Neither of these seem to work. I also tried doing the
RAILS_DEFAULT_LOGGER.debug "rendering html", and I also added to it so
that I can see what some of the request headers:
RAILS_DEFAULT_LOGGER.debug "rendering script; accepts:#
{request.accepts}, method:#{request.method}"
and lo and behold: it's rendering HTML, and simply passing right over
the js area. Here's what the logger outputted:
The first submit, where the entry is initially saving:
Processing BlogEntriesController#create (for 127.0.0.1 at 2009-02-22
23:46:30) [POST]
Session ID: fun!
Parameters: {"authenticity_token"=>"fun!", "blog_entry"=>
{"title"=>"asdf", "content"=>"asdf"}}
[4;35;1mUser Columns (3.6ms) SHOW FIELDS FROM `users`
[4;36;1mUser Load (0.3ms) SELECT * FROM `users` WHERE (`users`.`id`
= 1) LIMIT 1
[4;35;1mBlog Load (0.9ms) SELECT * FROM `blogs` WHERE
(`blogs`.user_id = 1) LIMIT 1
[4;36;1mBlog Columns (3.5ms) SHOW FIELDS FROM `blogs`
[4;35;1mBlogEntry Columns (1.6ms) SHOW FIELDS FROM `blog_entries`
[4;36;1mSQL (1.6ms) BEGIN
[4;35;1mBlogEntry Create (0.4ms) INSERT INTO `blog_entries`
(`last_edited_at`, `updated_at`, `title`, `published`, `blog_id`,
`summary`, `content`, `first_published_at`, `flag_count`,
`created_at`) VALUES(NULL, '2009-02-23 04:46:30', 'asdf', 0, 1, NULL,
'asdf', NULL, NULL, '2009-02-23 04:46:30')
[4;36;1mSQL (0.5ms) COMMIT
rendering script; accepts:text/javascripttext/htmlapplication/xml*/*,
method:post
Rendering blog_entries/create
Completed in 50ms (View: 2, DB: 13) | 200 OK [http://localhost/
blog_entries]
Now the second submit, where it's supposed to be putting (and seems to
be):
Processing BlogEntriesController#update (for 127.0.0.1 at 2009-02-22
23:48:59) [PUT]
Session ID: dogs
Parameters: {"commit"=>"Save", "authenticity_token"=>"yay",
"id"=>"64", "blog_entry"=>{"title"=>"asdf", "content"=>"asdf"}}
[4;35;1mUser Columns (16.9ms) SHOW FIELDS FROM `users`
[4;36;1mUser Load (2.2ms) SELECT * FROM `users` WHERE (`users`.`id`
= 1) LIMIT 1
[4;35;1mBlogEntry Columns (1.8ms) SHOW FIELDS FROM `blog_entries`
[4;36;1mBlogEntry Load (0.4ms) SELECT `blog_entries`.* FROM
`blog_entries` INNER JOIN blogs ON blog_entries.blog_id = blogs.id
WHERE (`blog_entries`.`id` = 64 AND ((`blogs`.user_id = 1)))
[4;35;1mSQL (0.1ms) BEGIN
[4;36;1mSQL (0.1ms) COMMIT
rendering html; accepts:text/htmlapplication/xml*/*, method:put <---
**** __THERE'S THE PROBLEM__ ****
Redirected to #<BlogEntry:0x246321c>
Completed in 143ms (DB: 22) | 302 Found [http://localhost/blog_entries/
64]
And, since it redirects as stated, there's also the rendering of the
[GET] after that. This is really stumping me. I'm beginning to
wonder if anyone else has actually tried this sort of thing and
succeeded. The thing is that I'm trying to be RESTful, so I'm trying
to use the model/controller links that are already there, because this
is the first app I'm building and I want to do it right.
I also thought that maybe it was because I was using the
remote_form_for helper, so I changed the form to use the
form_remote_tag helper. The same as before ensued. So for some
reason, when the form is being submitted the second time, jQuery
forgets to tell Rails that it accepts javascript? I really don't
understand why this is happening :(
So the question is, how do I get the correct accept header sent the
second time? And WHY THE HECK IS IT CHANGING!?! It's the same freakin
form for goodness sake!!
Thank you again for bearing with me, as I'm new, and I hope we can get
this figured out.
Groove
On Feb 22, 7:32 pm, "s.ross" <[email protected]> wrote:
> You're looking at the form. And it's not surprising it looks the same
> because you wrote it to be that way. Here's my guess, and I can't tell
> without (essentially) being you, watching your http requests, etc.
> Here's the flow of what happens when you submit your form:
>
> You:
> Think you did a PUT via XMLHttpRequest
> Your Browser:
> Creates an xhr object and actually POSTs the form with a hidden
> field that contains
> the n/v pair _method=put
> Rails (not necessarily in this order):
> Gets the request and sees it is a POST
> Parses the headers, especially the Accept headers
> Looks for the hidden field, to see how it should be routed
> Sees it and routes to your update method
> Your update method
> does a respond_to |format|
> respond_to
> Looks through the Accept headers to see if it can figure out what
> the browser
> request is looking for.
>
> In actual practice, you have to get a number of things exactly right
> to make this happen. These are far easier to get right if you are
> using Prototype but no matter. If you are using jQuery's Ajax function
> (jQuery is a great choice too), I've seen inconsistencies in how the
> Accept header is set as compared to Prototype. The things that need to
> go right are:
>
> 1. Be sure before each Ajax call, the Accept header is set. E.g.:
>
> jQuery.ajaxSetup({
> 'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/
> javascript")}
>
> });
>
> 2. In Rails, make sure your headers are sorted so that the
> embarrassing ones are
> parsed out early. I'm not sure why you have a frozen array -- you
> didn't mention
> a Rails version, but I have 2.3 running with that sorting snipped
> just fine.
>
> If you are not sure which code is being executed, work backwards, and
> put RAILS_DEFAULT_LOGGER.debug "rendering script" in your script
> section and RAILS_DEFAULT_LOGGER.debug "rendering html" in your html
> section. See if you are really running the code you think you are.
>
> Finally, if you want to make this not happen again, write tests or
> specs to ship requests with these headers to your controller and make
> certain it renders the right content.
>
> Hope this helps.
>
> On Feb 22, 2009, at 11:58 AM, groovetrain wrote:
>
>
>
> > This is a long one, but it should be easy to parse through.
>
> > Thank you for the reply. A couple of things:
>
> > 1) The problem is with Firefox, that's what I'm using for development.
> > 2) The above code (and the code from the codetunes.com site) give the
> > error "can't modify frozen array"
>
> > I set it as a before filter in my blog_entries controller.
>
> > This does bring up a few things. Does this mean that the reason for
> > my problem is that Rails is not detecting that the browser is sending
> > and xhr request, and thus assuming that it is html? This is strange
> > to me because it's literally the same form. I ran a test where I left
> > the form alone, and didn't try to change to form so that it would PUT/
> > UPDATE, I left it as POST/CREATE, and Rails returned the js file, not
> > the html file.
>
> > In my javascript returned from the "create" method, I am modifying the
> > form that that it looks just the same as the form that is generated
> > when I am editing an entry. It may be something that I'm doing, I
> > don't know. I'll post the HTML and javascript (according to firebug)
> > for fun:
>
> > HTML of the create form before I submit it for the first time:
> > /* ---------------------------------- BEGIN */
> > <form action="/blog_entries" class="new_blog_entry"
> > id="submit_blog_entry" method="post" onsubmit="$.ajax({data:$.param($
> > (this).serializeArray()) + '&authenticity_token=' +
> > encodeURIComponent('3ec895eb98a99ed6f134a6112d93005e50f2834b'),
> > dataType:'script', type:'post', url:'/blog_entries'}); return false;">
> > <div style="margin: 0pt; padding: 0pt;">
> > <input name="authenticity_token"
> > value="3ec895eb98a99ed6f134a6112d93005e50f2834b" type="hidden">
> > </div>
> > <div class="indentUppercase">Title</div>
> > <input class="inputLarge onehundredpercent" id="blog_entry_title"
> > name="blog_entry[title]" size="30" type="text">
> > <br>
> > <div class="indentUppercase">Post</div>
> > <textarea cols="40" id="blog_entry_content" name="blog_entry
> > [content]" rows="20"></textarea>
> > <table width="100%">
> > <tbody><tr>
> > <td>
> > <table>
> > <tbody><tr>
> > <td class="blueControl" id="savePost"><input
> > class="blueControl" name="commit" value="Save" type="submit"></td>
> > <td class="blueControl" id="saveAndUnpublish"
> > style="display: none;">Save & Unpublish</td>
> > <td class="blueControl" id="publishPost">Publish</td>
> > </tr>
> > </tbody></table>
> > </td>
> > <td class="right" width="50">
> > <table>
> > <tbody><tr>
> > <!-- this is the button to replace this html and generate a new
> > partial for a new blog entry -->
> > <td class="blueControl" id="newPost"><input onclick="$
> > ("form#submit_blog_entry").block_this(); $.ajax
> > ({data:'authenticity_token=' + encodeURIComponent
> > ('3ec895eb98a99ed6f134a6112d93005e50f2834b'), dataType:'script',
> > type:'post', url:'/dashboard/new_blog_entry'});" value="new"
> > type="button"></td>
> > </tr>
> > </tbody></table>
> > </td>
> > </tr>
> > </tbody></table>
> > <br> </form>
> > /* ---------------------------------- END */
>
> > Here is the javascript that is returned that changes the form:
> > /* ---------------------------------- BEGIN */
> > $("#status").show();
> > $("#status").html("Blog entry was successfully created.");
> > $(".blockMe").unblock();
> > $("#status").fadeOut(3000);
> > if($("#submit_blog_entry").find("input[name*='method']").length == 0)
> > $("#submit_blog_entry").find("input
> > [name*='authenticity_token']").parent().prepend("<input type=\"hidden
> > \" name=\"_method\" value=\"put\" />");
> > else
> > $("#submit_blog_entry").find("input[name='_method']").val("put");
> > $("#submit_blog_entry").attr("action", "/blog_entries/50");
> > var onsub = $("#submit_blog_entry").attr("onsubmit");
> > $("form#submit_blog_entry").removeAttr("onsubmit");
> > $("form#submit_blog_entry").removeAttr("onSubmit");
> > onsub = onsub.replace(/url:'\/blog_entries/g, "url:'/blog_entries/
> > 50");
> > $("form#submit_blog_entry").submit(function(){ eval(onsub); } );
> > $("form#submit_blog_entry").attr("onsubmit", onsub);
> > alert(onsub);
> > /* ---------------------------------- END */
>
> > And here is the html of the form after that return:
> > /* ---------------------------------- BEGIN */
>
> > <form action="/blog_entries/50" class="new_blog_entry"
> > id="submit_blog_entry" method="post">
> > <div style="margin: 0pt; padding: 0pt;">
> > <input name="_method" value="put" type="hidden">
> > <input name="authenticity_token"
> > value="3ec895eb98a99ed6f134a6112d93005e50f2834b" type="hidden">
> > </div>
> > <div class="indentUppercase">Title</div>
> > <input class="inputLarge onehundredpercent" id="blog_entry_title"
> > name="blog_entry[title]" size="30" type="text">
> > <br>
> > <div class="indentUppercase">Post</div>
> > <textarea cols="40" id="blog_entry_content" name="blog_entry
> > [content]" rows="20"></textarea>
> > <table width="100%">
> > <tbody><tr>
> > <td>
> > <table>
> > <tbody><tr>
> > <td class="blueControl" id="savePost"><input
> > class="blueControl" name="commit" value="Save" type="submit"></td>
> > <td class="blueControl" id="saveAndUnpublish"
> > style="display: none;">Save & Unpublish</td>
> > <td class="blueControl" id="publishPost">Publish</td>
> > </tr>
> > </tbody></table>
> > </td>
> > <td class="right" width="50">
> > <table>
> > <tbody><tr>
> > <td class="blueControl" id="newPost"><input onclick="$
> > ("form#submit_blog_entry").block_this(); $.ajax
> > ({data:'authenticity_token=' + encodeURIComponent
> > ('3ec895eb98a99ed6f134a6112d93005e50f2834b'), dataType:'script',
> > type:'post', url:'/dashboard/new_blog_entry'});" value="new"
> > type="button"></td>
> > </tr>
> > </tbody></table>
> > </td>
> > </tr>
> > </tbody></table>
> > <br> </form>
> > /* ---------------------------------- END */
>
> > You may notice that the onsubmit attribute doesn't contain anything,
> > however, a quick call using:
>
> > $("form#submit_blog_entry").attr("onsubmit");
>
> > yields:
>
> > $.ajax({data:$.param($(this).serializeArray()) +
> > '&authenticity_token=' + encodeURIComponent
> > ('3ec895eb98a99ed6f134a6112d93005e50f2834b'), dataType:'script',
> > type:'post', url:'/blog_entries/50'}); return false;
>
> > Which is exactly what it's supposed to be (or so it seems), when I
> > look at the onsubmit for the form generated from the partial for an
> > edit, it looks like
>
> > $.ajax({data:$.param($(this).serializeArray()) +
> > '&authenticity_token=' + encodeURIComponent
> > ('3ec895eb98a99ed6f134a6112d93005e50f2834b'), dataType:'script',
> > type:'post', url:'/blog_entries/46'}); return false;
>
> > exactly the same, but this one actually returns the js format, not the
> > html
>
> > Anyone have any ideas? Thanks again for the help thus far.
>
> > Groove
>
> > On Feb 22, 1:51 am, "s.ross" <[email protected]> wrote:
> >> This may be the browser-dependency issue with recognition of accept
> >> headers. I adapted a snippet that you can put in your
> >> ApplicationController:
>
> >> protected
> >> def correct_safari_and_ie_accept_headers
> >> ajax_request_types = ['text/javascript', 'application/json',
> >> 'text/xml']
> >> request.accepts.sort!{ |x, y| ajax_request_types.include?
> >> (y.to_s) ? 1 : -1 } if request.xhr?
> >> end
>
> >> What it does is sort the content types the browser claims to accept
> >> so
> >> that the ajax'ey
>
> ...
>
> read more »
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---