1

I have generated Admin namespaced Controllers for all my default models as follows:

rails g scaffold_controller admin/categories name:string slug:string description:string icon_xlarge:string icon_large:string icon_medium:string icon_small:string status:integer

This generated the following files:

Harshas-MacBook-Pro:nomad harshamv$ rails g scaffold_controller admin/categories name:string slug:string description:string icon_xlarge:string icon_large:string icon_medium:string icon_small:string status:integer
Plural version of the model detected, using singularized version. Override with --force-plural.
      create  app/controllers/admin/categories_controller.rb
      invoke  erb
      create    app/views/admin/categories
      create    app/views/admin/categories/index.html.erb
      create    app/views/admin/categories/edit.html.erb
      create    app/views/admin/categories/show.html.erb
      create    app/views/admin/categories/new.html.erb
      create    app/views/admin/categories/_form.html.erb
      invoke  test_unit
      create    test/controllers/admin/categories_controller_test.rb

app/model/category.rb

class Category < ActiveRecord::Base

  extend FriendlyId

  friendly_id :name, use: :slugged

  has_and_belongs_to_many :venues

end

app/controller/admin/categories_controller.rb

class Admin::CategoriesController < ApplicationController
  before_action :set_admin_category, only: [:show, :edit, :update, :destroy]

  # GET /admin/categories
  def index
    @admin_categories = Admin::Category.all
  end

  # GET /admin/categories/1
  def show
  end

  # GET /admin/categories/new
  def new
    @admin_category = Admin::Category.new
  end

  # GET /admin/categories/1/edit
  def edit
  end

  # POST /admin/categories
  def create
    @admin_category = Admin::Category.new(admin_category_params)

    if @admin_category.save
      redirect_to @admin_category, notice: 'Category was successfully created.'
    else
      render :new
    end
  end

  # PATCH/PUT /admin/categories/1
  def update
    if @admin_category.update(admin_category_params)
      redirect_to @admin_category, notice: 'Category was successfully updated.'
    else
      render :edit
    end
  end

  # DELETE /admin/categories/1
  def destroy
    @admin_category.destroy
    redirect_to admin_categories_url, notice: 'Category was successfully destroyed.'
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_admin_category
      @admin_category = Admin::Category.find(params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def admin_category_params
      params.require(:admin_category).permit(:name, :slug, :description, :icon_xlarge, :icon_large, :icon_medium, :icon_small, :status)
    end
end

app/view/admin/categories/index.html.erb

<h1>Listing admin_categories</h1>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Slug</th>
      <th>Description</th>
      <th>Icon xlarge</th>
      <th>Icon large</th>
      <th>Icon medium</th>
      <th>Icon small</th>
      <th>Status</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @admin_categories.each do |admin_category| %>
      <tr>
        <td><%= admin_category.name %></td>
        <td><%= admin_category.slug %></td>
        <td><%= admin_category.description %></td>
        <td><%= admin_category.icon_xlarge %></td>
        <td><%= admin_category.icon_large %></td>
        <td><%= admin_category.icon_medium %></td>
        <td><%= admin_category.icon_small %></td>
        <td><%= admin_category.status %></td>
        <td><%= link_to 'Show', admin_category %></td>
        <td><%= link_to 'Edit', edit_admin_category_path(admin_category) %></td>
        <td><%= link_to 'Destroy', admin_category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Category', new_admin_category_path %>

My Attempts

I edited the Controller as below

 # GET /admin/categories
  def index
    @admin_categories = Category.all
  end

  # GET /admin/categories/1
  def show
  end

  # GET /admin/categories/new
  def new
    @admin_category = Category.new
  end

  # GET /admin/categories/1/edit
  def edit
  end

  # POST /admin/categories
  def create
    @admin_category = Category.new(admin_category_params)

    if @admin_category.save
      redirect_to @admin_category, notice: 'Category was successfully created.'
    else
      render :new
    end
  end

When I go to localhost/admin/categories and click "NEW category", I get the following error now:

My routes file:

Rails.application.routes.draw do

  # Admin Routing
  namespace :admin do
    resources :categories, :cities, :countries, :lists, :oauths, :regions, :tags, :users, :user_groups, :venues, :venue_photos, :venue_reviews
  end

end
Harsha M V
  • 54,075
  • 125
  • 354
  • 529

3 Answers3

1

To access models outside of the namespace you need to call ::Category.new instead of Admin::Category.new

Arctodus
  • 5,743
  • 3
  • 32
  • 44
1

You have resources :categories defined under the namespace :admin in your routes.rb,so this line in your views/admins/categories/_form.html.erb

<%= form_for(@admin_category) do |f| %>

should be

<%= form_for([:admin, @admin_category]) do |f| %>

For more info,refer this API

Update

The second error is because of this line

params.require(:admin_category).permit(:name, :slug, :description, :icon_xlarge, :icon_large, :icon_medium, :icon_small, :status)

It should be

params.require(:category).permit(:name, :slug, :description, :icon_xlarge, :icon_large, :icon_medium, :icon_small, :status)
Pavan
  • 33,316
  • 7
  • 50
  • 76
  • am using namescope for the first time and its pretty confusing even after reading through the document. I change params.require(:category).pemit(....) and ended up with this error :( http://i.imgur.com/Faa3dIS.jpg – Harsha M V Jun 18 '14 at 18:42
  • @Harsha MV What happens when you change this line `redirect_to @admin_category` to `redirect_to admin_categories_url` in your `create action`? – Pavan Jun 18 '14 at 19:04
  • You can also try `redirect_to admin_category_path(@admin_category)` The scaffold generator expected the Category model to be namespaced, that's why you need to adjust a few things. – Arctodus Jun 18 '14 at 19:05
  • damn looks like i can just write my controllers manually its easier :( Both dont seem to work. same error – Harsha M V Jun 18 '14 at 19:12
  • @HarshaMV Good luck with that.Good night :) – Pavan Jun 18 '14 at 19:13
  • @Pavan hehe :D thanks buddy :D i will get back if i find any issues :( – Harsha M V Jun 18 '14 at 19:16
  • thanks guys. simplified the routes and variables and seem to be working :D – Harsha M V Jun 19 '14 at 06:07
1

As your error indicates, this is an issue with how you are calling your form. Your form needs to reference the admin namespace, like this:

<%= form_for [:admin, @category] do |f| %>

However, there are a number of things different about the way the scaffold built your docs from how I would recommend.

I would also suggest simplifying the code in the scaffolded controller to reference simple @category and @categories, rather than @admin_category and @admin_categories.

Also, the model should not be in the admin namespace, so Admin::Category.new should be Category.new. The rest of the model calls should be adjusted accordingly as well.

steel
  • 11,883
  • 7
  • 72
  • 109
  • 1
    Try heading into your controller file and changing require(:admin_category) to require(:category). – steel Jun 18 '14 at 18:40
  • are these error because am using the model outside the namescope? i am trying to simply it. http://i.imgur.com/Faa3dIS.jpg – Harsha M V Jun 18 '14 at 18:45
  • You'll just need to do some more editing to clear this up. To fix the most recent issue, try redirect_to admin_category_path(@category), if you're using my variable name suggestions. – steel Jun 18 '14 at 18:47
  • If you're still getting weird errors losing patience, maybe take a breather and work through this tutorial: http://everydayrails.com/2012/07/31/rails-admin-panel-from-scratch.html – steel Jun 18 '14 at 18:51
  • freak :D i think i need a breather big time. thanks a lot for the link i will check it out :D – Harsha M V Jun 18 '14 at 18:54