Sunday, January 06, 2008

AWDR - Part 2 Screencast Clarifications

In this screencast, admin namespaced products controller is created. This gives products_controller in the admin directory.

The public products_controller has only index and show and the other actions are not needed so we delete them. The admin products controller is capable of all the CRUD operations. So we copy them over to the admin controller. The urls needs to be changed because when we access them as an admin the url on the browser will be:

http://localhost:3000/admin/products

Since we have admin in the url the url helper methods will be different than the public url methods. You can find out the name of the URL methods by doing rake routes in your rails project home directory. Here is the process:

1. Let's say you want to find out the name of the URL helper method to route to update action in admin's products controller.
2. Look at the output of rake routes and find the controller with value admin/products and action with value show. The first column will give you the name of the URL helper you should use in the admin/products_controller and the views under admin/views/ directory.
3. The :id in the /admin/products/:id is a variable that is available in the params hash. You can access it's value in your controller by doing params[:id]. Similarly if you find anything in the second column that is prefixed by : , it means that it is a variable and you can extract it's value in the controller from the params hash.

4 comments:

  1. Thanks for the clarifications.

    I created an admin folder under views and I moved the Edit, New, and Show and products templates there.

    Everything works fine but the "Back" and "Show" links at the bottom of the screens. What did I miss?

    Thanks.

    -- Jim

    ReplyDelete
  2. Hi,

    By using the application.html.erb layout file for the admin section you have introduced a bug in the application.

    This happens in application layout. You must put a check around the hidden_div_if method (or totally refactor the application code) so that it won't use @cart that doesn't get initialized in the admin section. And moreover the _cart and _cart_item partials don't exist either.

    This bug would have been spotted earlier on using functional tests, because now code refactoring is required which is never very funny.

    Beside that thank you very much for providing these screencasts to make the Depot app work with Rails 2.0

    ReplyDelete
  3. What Erik says is true about the application view breaking in the admin namespace. However, a simple check on the status of @cart can easily solve this problem. I just threw an unless statement around the cart rendering portion. Here's what I'd do:

    %unless @cart.nil?%
    partial cart code here
    %end%

    It's more of a hack than a solution since there are larger design issues to consider, but since the cart is the only model that shouldn't be in viewed in the admin portion, I don't think a complete refactoring is in order. Anyway, I've learned a lot from these tutorials (I just finished checkout and am working on the last few chapters of depot from the book). So thank you very much for your screencasts!

    ReplyDelete
  4. when I type
    "script/generate controller admin::products"
    I get
    " The name 'Admin::ProductsHelper' is either already used in your application or reserved by Ruby on Rails.
    Please choose an alternative and run this generator again.
    what is up?

    ReplyDelete