Hm. Well I was trying to do this using as many rails helpers as possible. The site I'm building should only use one javascript framework, and jQuery is it. In the past, I've user the ajaxForm jQuery plugin in the past with great success. I need to stop being so idealistic. The reason I felt free to use rails js helpers was because I'm using the jRails plugin (http://ennerchi.com/projects/ jrails) and it seems to be working. Is it still not ok to mix'n'match do you think?
I'll do my homework and get back to you. Groove On Feb 24, 1:47 am, "s.ross" <[email protected]> wrote: > I'm not sure why you are using form_remote_tag, but the Javascript > helpers really only work with Prototype, not jQuery. You should be > using the AjaxForm jQuery plugin and form_for or form_tag. If you use > AjaxForm, you will have accomplished exactly one thing: Getting the > submit() event to use xhr. That's step 1. Step 2 is customizing the > behavior of the submit event depending on whether it's new (create) or > existing (update). For that, you can ship back script from Rails but > don't use the Rails js helpers. You just can't mix and match. > > Read this:http://errtheblog.com/posts/73-the-jskinny-on-jquery > > Not the newest, but still good reading for the Rails-jQuery issue. > > I believe if you look at the DOM constructed from your existing code > in Firebug, you'll discover that things aren't quite lining up right > after a successful submit event. > > Are you doing any other jQuery stuff to respond to submit, click, or > other events that could influence these behaviors? > > --s > > On Feb 23, 2009, at 4:20 PM, groovetrain wrote: > > > > > Here's the code for the partial that renders the form: > > >http://pastie.org/397999 > > > Here's the rjs that's returned when the above form is submitted and > > the create first happens, that's supposed to be changing the form from > > a create to an update (which is IS doing): > > >http://pastie.org/398003 > > > Here's the relavant javascript/jQuery in the main js file: > > >http://pastie.org/398008 > > > Hope this helps. If you need more code, let me know. > > > Groove > > > On Feb 23, 12:45 am, groovetrain <[email protected]> wrote: > >> 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" > > ... > > 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 -~----------~----~----~----~------~----~------~--~---

