0

I have an app in Rails with a form to edit a register, and I would like to have a destroy button here.

I currently have the button with code that I think should work. this is how it looks in the view and this is the code

 <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
                                       register_path(register),
                                       :method => :delete,
                                       :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
                                       :class => 'btn btn-mini btn-danger' %>

But, when I try to delete it says "ActiveRecord::RecordNotFound in RegistersController#edit"

4
  • 1
    did you try using the button_to method in rails? <%= button_to "delete", {:controller => :name, :action => 'destroy', :id => name.id }, :method => :delete %>
    – coletrain
    Oct 5, 2012 at 0:48
  • I don't think it's relevant, but why are you putting the :confirm and :class options in :data? That doesn't look right. Oct 5, 2012 at 0:53
  • 1
    Where does your destroy action redirect to after destroy? The error message seems to suggest it goes back to edit where the register object that is being edited has just been destroyed.
    – John
    Oct 5, 2012 at 1:55
  • The register is not destroyed at all. When I refresh the edit page it is still there.
    – Phsyckr
    Oct 5, 2012 at 14:10

1 Answer 1

0

try this, write in your RegistersController#destroy actin

def destroy
 @register = Register.find(params[:id])
 if @register.present?
   @register.destroy 
   redirect_to .....
 else
   redirect_to .... or render 'xyz'
 end
end

i thing, error 'ActiveRecord::RecordNotFound' comes because record is not present in the database.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.