Search code examples
rubyruby-on-rails-4controlleractionmodels

howto generate associated admin models in rails


i've an rails application where the CUD (create, update, delete) actions only can used by the admin to create update or delete an item. The show (read) action can used by everyone.

Now i want that only the admin create other entries. this is my item model

    class Item < ActiveRecord::Base
    has_many :entries 

And this is my entry model

    class Entry < ActiveRecord::Base
    belongs_to :item

In my admin_controller for the items i have the need actions.

    class Admin::ItemsController < ApplicationController

Now i have two questions

  1. My question is, have i to define an own admin entry controller? I think yes, there is an own admin-item-controller needed.

  2. How is the best way to define that the item_admin is allow to create, update or delete the entries. everyone else can the entries only see. can every help me please?


Solution

  • I think there are two options:

    1. Build an admin/items and an admin/entries controller that allow all CRUD operations - but for admins only. In addition have a public items and an public entries controller that have only the show method implemented.
    2. Have only one controller per type (no dedicated admin controller). Within this controller use gems like pundit or cancancan to ensure that only admins can use the create, update and destroy action.