views:

162

answers:

1

I was putting together a quick inline editing feature in my first Rails app and just as I was getting it working it occurred to me that I may be violating RESTful principles. The edit updated an image name. To do so, it submits, via PUT to Image#update and passes the new modified name as image[name].

The database gets updated properly, but I need that value back so that my markup can reflect the name change. To do that, I was calling /images/:id.json, but that got me wondering whether a PUT request can "validly" (in that RESTful sort of way) return a value like this.

Thoughts?

Update: For whatever it's worth, I'm using jQuery and the jEditable plugin to do the inline editing. Here's my jEditable code:

$(document).ready( function() {
  $('h2').editable(
    '/images/' + $('#image-id').val() + '.json',
    {
      method: 'PUT',
      name:   'image[name]',
      submitdata: { authenticity_token: $('#auth-token').val() },
      submit: 'Save',
      cancel: 'Cancel'
    }
  );
})

And my Image#update method as it exists right now:

def update
  @image = Image.find( params[:id] )
  if @image.update_attributes( params[:image] )
    flash[:notice] = "Successfully updated image."

    respond_to do |format|
      format.html { redirect_to @image }
      format.json { render :json => @image.to_json }
    end
  else
    render :action => 'edit'
  end
end
A: 

If your concern is just that your update method with JSON provide a response body and not just a 200 OK (Rails's head :ok) then I don't think you need to be worried. The default response is 200 OK so the only difference between what you're doing and what Rails does by default (in its scaffolds) is that you're also including a response body. As far as I can tell proper REST etiquette only requires that you return a 200 OK and doesn't care about the response body, which is in line with what you're doing.

Beyond that all your code looks excellent.

Peter Wagenet
Thanks. I don't want to get overly pedantic or puritanical about being RESTful, but I'd like to shoot for purity where I can and scale back where I have to.
Rob Wilkerson