I am trying to create a destroy link to my users controller, I am also using devise.
Here is my code -
<%= link_to 'Delete User?', child, :confirm => "Are you sure you want to delete #{child.full_name}?", :method => :delete, :class => "user-additional", :style => "font-size:12px;font-weight:normal;" %>
def destroy
if @user = User.find(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to account_index_path }
format.xml { head :ok }
end
end
end
devise_for :users
resources :users, :except => [:new]
The link translates to localhost:3000/users/10
When clicked this opens the users show instead of deleting them
Any ideas ?
Destructive actions should be performed as a form submission - http://www.w3.org/2001/tag/doc/whenToUseGet.html#checklist
use button_to
(passing a :method => :delete
) instead and style the button appropriately.
Actually I just had the exactly same problem yesterday
Try this:
<%= button_to "delete", your_object, :method=>:delete, :class=>:destroy %>
It works (for me at least)
In case that you are using jQuery instead of Prototype, you are probably missing a javascript file.
You can find details on how to add it to your project from the jquery-ujs GitHub page or from episode 205 of the Railscasts.
At a guess I think it is because in Rails 3, unobtrusive javascript is now used for functionality such as this (Rails 2 would output a bunch of nasty inline javascript for your code, Rails 3 puts the javascript in an external file, and uses HTML5 data-
attributes to interact with that.)
To solve this you need to include <%= csrf_meta_tags %>
in your page header to reference the external javascript. It also deals with XSS issues.
Some details here: Delete link sends "Get" instead of "Delete" in Rails 3 view
If you haven't included jquery and jquery-ujs in your app , the default link_to default coming with scaffold wont work!
I had the same issue.It got solved after including both these js!
Also if you get this problem in production mode, it may be because you have not compiled the assets. See http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets
为我工作的确认信息。
<%= button_to 'Destroy', {action: :destroy, id: version.id}, onclick: 'return confirm("Are you sure?")', method: :delete %>
If you are using jQuery, make sure you have something like this:
<script type="text/javascript">
// this allows jquery to be called along with scriptaculous and YUI without any conflicts
// the only difference is all jquery functions should be called with $j instead of $
// e.g. $jQ('#div_id').stuff instead of $('#div_id').stuff
var $jQ = jQuery.noConflict();
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.