I need to make a button to destroy object.
Instead of destroy it shows fields
inex.html.erb
<%= link_to image_tag("/images/glossy_green_button.png"), device , { :html => { :method => :delete}, :controller => :devices, :action => 'destroy',:id => device.id, :onclick => 'return confirm(\'Are you sure?\');' }, :method => :turnon %>
devices_controller.rb
def destroy
#device = Device.find(params[:id])
#device.destroy
respond_to do |format|
format.html { render action: "destroy" }
format.json { head :no_content }
end
end
routes.rb
device GET /devices/:id(.:format) devices#show
PUT /devices/:id(.:format) devices#update
DELETE /devices/:id(.:format) devices#destroy
Appreciate any ideas what I got wrong.
Thank you
D
UPDATED:
<%= button_to "Delete", device , :method => :delete %>
this works fine
Which version of Rails are you using? Why your link_to method calling is so complex? It can be simply re-written. Try the following one:
<%= link_to image_tag("/images/glossy_green_button.png"), device , :method => :delete, :confirm => "Are you sure?"
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 of :delete) instead and style the button appropriately.
or Try this <%= button_to "delete", your_object, :method=>:delete, :class=>:destroy %>
Related
I would like to call partials on some standard operations. I am using this method for calling the partial:
%li= link_to 'Delete Event', 'javascript:void(0);', :class => 'alert tiny button', :data => {'reveal-id' => :RevealDelete}
= render 'layouts/reveal_delete', :item => event_display(#event.event), :resource => #event
Then in my partial,
#RevealDelete.reveal-modal
%a.close-reveal-modal ×
%h3= "Delete #{item}"
%p Are you sure you want to delete this?
=link_to "Delete #{item}", resource, :method => :delete, :remote => :true, :confirm => resource, :class => 'button close-reveal-modal'
%a.button.alert.close-reveal-modal Cancel
How can I have this has as something like:
link_to 'Delete', '#', :partial => 'layouts/delete', :remote => :true?
so that I only render that partial when clicked and not when the page loads?
You can do that with javascript like:
<%= link_to "Delete", delete_content_path, :remote => true %>
The action in your corresponding controller then will be this:
My Controller:
def delete_content
respond_to do |format|
format.js
end
end
Then you can create the delete_content.js.erb inside your correct directory of the link and there you put the following code:
delete_content.js.erb
$('#div_id').html("<%= render :partial => 'my_partial' %>");
Then in your view:
delete_content.html.erb
<div id = "div_id">
#this div is html div that will render your partial
</div>
Don't forget to put your partial _my_partial.html.erb in the same folder.
To add to the accepted answer, I only got it to work after changing the js portion to the following:
$('#div_id').html("<%= escape_javascript(render :partial => 'my_partial') %>");
Without the escape_javascript it was just rendering the partial in the background and not updating the view.
on the view do this:
link_to "Delete #{item}", '/model/confirm_deletion', :method => :delete, :remote => true #add the class and extra attributes if neeeded
on your controller
def confirm_deletion
end
and add a view to the confirm_deletion action in js
#RevealDelete.reveal-modal
%a.close-reveal-modal ×
%h3= "Delete #{item}"
%p Are you sure you want to delete this?
=link_to "Delete #{item}", resource, :method => :delete, :remote => :true, :confirm => resource, :class => 'button close-reveal-modal'
%a.button.alert.close-reveal-modal Cancel
:javascript
$(body).append($('#RevealDelete'));
that would make an ajax request to load that custom confirmation dialog, maybe you want to add some wrapper to insert the dialog instead of using body.append
I'm having problems with rails 3.2.8 and ajax callbacks... it seems those are not firing at all... I've tried binding with Jquery code (I can see the js script on the chrome toolbox) and I've even tried putting the :success => 'alert("bla")' on the link_to line, but it still doesn't do anything... the controller works since my row is actually deleted... but can't bindd to the callbacks! please help!
Here is my view line:
<td><%= link_to 'Destroy', pet, :method => :delete, :remote=>true, :class => 'delete_pet' %></td>
Here is my controller action
def destroy
#pet = Pet.find(params[:id])
#pet.destroy
respond_to do |format|
format.html { redirect_to(pets_url) }
format.js { render :nothing => true }
end
end
and here is my js code:
jQuery(function($) {
$('.delete_pet').bind("ajax:before", function(){alert('bla2');})
.bind("ajax:success", function(){alert('bla2');})
.bind("ajax:failure", function(){alert('bla2');})
.bind("ajax:complete", function(){alert('bla2');});
});
replace
format.js { render :nothing => true }
with
format.json { render :nothing => true }
and also replace
<%= link_to 'Destroy', pet, :method => :delete, :remote=>true, :class => 'delete_pet' %>
with
<%= link_to 'Destroy', pet, :method => :delete, :remote=>true, 'data-type'=>'json', :class => 'delete_pet' %>
I've been having similar issues with the respond_to blocks. I've gotten around it temporarily by checking the request. For example, you're code would look like:
def destroy
#pet = Pet.find(params[:id])
#pet.destroy
if request.xhr?
# the request was received via an AJAX request
render :nothing => true
else
redirect_to(pets_url)
end
end
Not as elegant as the resond_to block, but it seems to reliably work as expected.
Good luck!
The problem was that on my application.js I was including manually a jquery file... like this..
//= require jquery-ui-1.8.23.custom
shame on me!
I have setup a admin namespace in order to access models in the admin area: /admin/pages
However i have the following problem
i cant get the delete function to work under Admin::PageController for example or any of my models.
Does anyone know how to do this.
I have the following:
Admin::PageController I have the following
def destroy
#page = Page.find(params[:id])
#page.destroy
respond_to do |format|
format.html { redirect_to admin_pages_url }
format.json { head :ok }
end
end
Then on my page index file where i want a link to delete the record i have the following: (/admin/pages)
<%=link_to admin_page_path(page), :class => 'ico del' do %>
<%='Delete'%>
<% end %>
Does not seem to work. Anyone know how to get this to work?
you have missed :method option in link_to call
link_to 'Delete', admin_page_path, :confirm => 'Are you sure?', :method => :delete
or
<%=link_to admin_page_path(page), :class => 'ico del',:method => :delete do %>
<%='Delete'%>
<% end %>
The link_to helper defaults to a GET request unless you specify additional attributes to tell it how you want it to be handled.
In this case, you need to set some extra arguments:
<%=link_to "Delete", admin_page_path(page), :class => "ico del", :remote => true, :method => :delete %>
What actually happens in the background is the Rails UJS (unobtrusive javascript adapter) captures the click event and sends the request via AJAX. So you should see it hit your server with a POST (but it passes in _method => delete as well) to delete the object.
I'm also assuming you have your routes set up correctly. Something like:
namespace :admin do
resources :pages
end
When I click on the delete link I created, it doesn't do anything (even the flash[:notice] part) in the controller. Am I not calling the .delete? part correctly? The POST part works as I can add tips.
Link:
<%= link_to "Delete", :controller => "/admin", :action => "tips", :id => t.id, :method => :delete, :confirm => "Are you sure?" %>
Admin Controller
def tips
#tips = Tip.all
if request.post?
tip = Tip.new(params[:geek_tips])
if tip.save
flash[:notice] = "Saved!"
redirect_to :action => "tips"
else
flash[:notice] = "Error!"
end
elsif request.delete?
tip = Tip.find_by_id(params[:id])
tip.delete!
flash[:notice] = "Delete Message"
redirect_to :action => "tips"
end
end
Design issues aside, I think that your :method option is being interpreted as a query param. Can you see "method" in the URL if you hover on the link?
If so, try...
<%= link_to "Delete", {:controller => "/admin", :action => "tips", :id => t.id}, :method => :delete, :confirm => "Are you sure?" %>
Note the braces around the part that defines the URL of the request.
Regarding the design: Any time you have multiple actions in one controller method there is probably a design issue. In this case, instead of using one admin controller method to do multiple tips actions I would consider making a dedicated tips_controller controller to map to your Tip model.
If you used RESTful routes, that is, in config.rb you set...
map.resources :tips
...then you could use the create and destroy methods in your tips_controller for creating and deleting your tips respectively.
I've ran into a ror problem using the link_to. Why does my link to use the GET method and my button_to use the POST method, after I specified my "method"=>"post" within the link_to parameters?
View:
<%= button_to "pdf", :action => 'getquote' %>
<%= link_to 'pdf', {:controller => 'inventories', :action => 'getquote', :method => :post } %>
Controller Method:
def getquote
#cart = find_cart
respond_to do |format|
format.pdf
end
end
Terminal Output (Button/Link, respectively):
Processing InventoriesController#getquote (for 127.0.0.1 at 2010-01-30 01:38:02) [POST]
Parameters: {"action"=>"getquote", "authenticity_token"=>"D2cwnHyTHgomdUM3wXBBXlOe4NQLmv1Srn0paLbExpQ=", "controller"=>"inventories"}
Processing InventoriesController#show (for 127.0.0.1 at 2010-01-30 01:39:07) [GET]
Parameters: {"method"=>"post", "action"=>"show", "id"=>"getquote", "controller"=>"inventories"}
I think your html options have to be in a separate hash from your url options:
<%= link_to 'pdf', {:controller => 'inventories', :action => 'getquote'}, {:method => :post } %>
I looked all over for a proper example, with no luck. For my code, I've mostly given up and just use the new style:
<%= link_to 'Delete', custom_event, :confirm => 'Are you sure?', :method => :delete %>
Might be useful for someone who is visiting :)
By default, button_to performs POST action only.
to do make a GET the syntax is as follows:
<%= button_to 'pdf', { :action => 'getquote'}, :method => :get %>
One possibility is that you have Javascript disabled, in which case it will fall back to a GET.