For the life of me I can't figure this one out, although I can't find
anyone else who's attempted to do this, and probably with good reason.

Context: blog using AJAX

What I'm trying to do: when the user initially saves a blog entry, or
when auto-saving, I want subsequent saves to not create a new blog
entry

Why I can't just reload the partial:
 - That would interrupt the flow of blog-writing
 - I'm using FCKEditor, and it takes a few seconds to pop in place
upon the reload of the partial (which is less than elegant)

The solution that I came up with was to use the returned and evaluated
javascript/jQuery to re-write parts of the form so that it would
submit to my blog_entries controller and be treated correctly.  So, I
have to change the method from POST to PUT, which in rails means
creating a hidden form element like so:

<input type="hidden" name="_method" value="put" />

and then changing the form action from

action="/blog_entries"
  to
action="/blog_entries/21" - or whatever the id is of the newly created
blog entry

There's also a little place in the onsubmit attribute that I'm also
changing from /blog_entries to /blog_entries/21

What's happening is it will submit, the updates will be successful,
but my update method will always respond with format.html, not
format.js, which is meaning I'm getting forwarded to my default
scaffold "show" page, which greets me with a nice "Your blog entry was
successfully updated."

My update method looks like:

  def update
    @blog_entry = @user.blog_entries.find(params[:id])

    respond_to do |format|
      if @blog_entry.update_attributes(params[:blog_entry])
        flash[:notice] = 'Blog entry was successfully updated.'
        format.js { redirect_to(:action => 'success') } if
request.xhr?
        format.html { redirect_to(@blog_entry) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @blog_entry.errors, :status
=> :unprocessable_entity }
      end
    end
  end

I've checked the server console, and it is receiving the PUT method,
and my blog entry is getting updated, but it doesn't make any sense to
me why it is always being redirected, seemingly skipping over the
format.js line every time.  I've tried this line like this, and each
of them yields the same thing:

        format.js { redirect_to(:action => 'success') }
        format.js if request.xhr?  (with the appropriate update.js.rjs
file in place)
        format.js

If anyone could help me figure this one out it would be great!  Sorry
about the long post, I wanted to be clear what was going on.

Groove
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to