Monday, 15 July 2013

Rails 4 uninitialized constant Admin::Category -



Rails 4 uninitialized constant Admin::Category -

i have generated admin namespaced controllers default models 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 next 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 model detected, using singularized version. override --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] # /admin/categories def index @admin_categories = admin::category.all end # /admin/categories/1 def show end # /admin/categories/new def new @admin_category = admin::category.new end # /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 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 updated.' else render :edit end end # delete /admin/categories/1 def destroy @admin_category.destroy redirect_to admin_categories_url, notice: 'category destroyed.' end private # utilize callbacks share mutual setup or constraints between actions. def set_admin_category @admin_category = admin::category.find(params[:id]) end # allow 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 |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 sure?' } %></td> </tr> <% end %> </tbody> </table> <br> <%= link_to 'new category', new_admin_category_path %> my attempts

i edited controller below

# /admin/categories def index @admin_categories = category.all end # /admin/categories/1 def show end # /admin/categories/new def new @admin_category = category.new end # /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 created.' else render :new end end

when go localhost/admin/categories , click "new category", next error now:

my routes file:

rails.application.routes.draw # admin routing namespace :admin resources :categories, :cities, :countries, :lists, :oauths, :regions, :tags, :users, :user_groups, :venues, :venue_photos, :venue_reviews end end

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

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

should be

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

for more info,refer api

update

the sec error because of 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)

ruby-on-rails ruby-on-rails-4 rails-generate

No comments:

Post a Comment