I'm trying to implement an admin control panel for my application and
everything seems to work fine, except when I try to call the destroy
method from the view. I really tried to search for a solution, reading
some routing rails articles, but can't figure out what's going on.
When I call the method from the console it works. Calling it from the
view redirects to the list action, but the product is not deleted.
I tried to call admin_products_path(product) like you suggested but it
shows me an error (You have a nil object ...). Calling
admin_product_path works but don't delete the product, it redirects to
the list action and the product remains there. Rails version is 2.1.1.
When I told you that I called the destroy method using the console,
what I did was execute "Product.find(3).destroy". Is there another way
to test a controller's method using the console ?
So let's back up one step. Your error is "You have a nil object...",
let's prove that is true.
How about changing the
<%= link_to .... %>
to something simple like
<p>Product ID: <%= product %></p>
Then, when that gives you nil cut the loop out and insert a statement
that displays something meaningful from @products. If that produces a
nil that you have to look at how you got here (in the view) and what
was your assumption how @products got to have a value associated with
it.
This would be easier for you if you single stepped through a
debugger. You might consider it time well spent to learn how.
OK Rick, I did what you said and confirmed that @products is
populated. That error only occurs when I use "admin_products_path". If
I change it to "admin_product_path" no error occurs but the product isn
´t deleted.
I will follow your hint later and try to debug the application to see
what is going wrong. I'll post the results.
I don't think your syntax is the problem here, [:admin, product] or
admin_product_path should work fine.
I'm afraid I can't work out what's going wrong but why not try adding
"logger.info('message here' )" to your controller both before and
after the destroy call.
You can then check your log and find out what code is being run and
what isn't. Also, do you have anything in your model that may be
causing an issue, a :before_destroy perhaps?
OK, first I tried to call logger.info ‘message’ but nothing was logged (development.log). After this I installed ruby-debug and put an “debugger” instruction but the execution didn’t stopped on that point. So I really believe that the destroy method isn’t being called, but why?
I know that is something really stupid but can’t see what’s wrong …
Ryan, the controller is ‘admin/products_controller.rb’.
I will try to fix it using the link Rick suggested.
You are right. I tried to do what you suggested with a new project and
it really worked. But with my project it doesn´t work. Something is
wrong with my project, I´ll try to discover later. Maybe something
with pagination or the model, I don´t know. If I discover something I
´ll post the results later.