views:

817

answers:

3

Hello,

I have a model posts where users can edit and delete their own posts. My index page is a table with a few columns for each post. One of them holds a form for the delete button. Is there any examples on how you jquery submit a post/delete form with rails? I need to pass the status back to the user. Eg. posts deleted, access denied etc and remove the row if successfull.

Best regards. Asbjørn Morell.

A: 

You should look into the link_to_remote method; it uses AJAX -- I assume that what you mean by 'JQuery'.

I normally do something like:

<%= link_to_remote "Delete", :url => some_path(@object), :method => :delete %>
Matt Darby
A: 

link_to_remote. Will create inline javscript that generates a post form. This will not work if javascript is disabled.

I have created a standard html form, that works without javascript, and would like to spice it up with jquery if javascript is enabled.

<% form_for @post, :html => { :method => 'delete' } do |f| %> <%= submit_tag "Delete", :name => "delete" %> <% end %>

I found this blob last night, and it covers almost what I would like to do. The only thing I can't figuere out, is how I return an error er success from the controller.

atmorell
+1  A: 

If the action is successful the response will be a successful one (i.e. 200 OK). In your jQuery you can add an error function if the request encounters an error:

error: function() {
  // the item has not been deleted
  // and we encountered an error
}

Then in your Controller if you want to explicitly return your own error:

render :text => 'Could not compute!', :status => :unprocessable_entity

Or any other error. Hope this helps.

marktucks
Hello,I ended up simple checking the flash[] variable.Controller:if @post.destroy flash[:note] = "Post was destroyed"endjs.erb<% if flash[:note] == "Post destroyed" %> Hide the row etc.<% end %>Display flash text
atmorell