0
votes

I am making a Admin area.

In app/controllers

application_controller.rb
public_controller.rb
admin_controller.rb
kategoris_controller.rb

And in app/controllers/admin

kategoris_controller.rb

My route file:

resources :kategoris
namespace :admin do
  resources :kategoris
end

My kategori index file in app/views/admin/kategories/index

<h1>Listing kategoris</h1>

<table>
  <tr>
    <th>Navn</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @kategoris.each do |kategori| %>
  <tr>
    <td><%= kategori.name %></td>
    <td><%= link_to 'Show', kategori %></td>
    <td><%= link_to 'Edit', edit_kategori_path(kategori) %></td>
    <td><%= link_to 'Destroy', kategori, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New Kategori', new_kategori_path %>

My kategori controller in Admin/kategoris_controller.rb

class Admin::KategorisController < Admin::AdminController
  # GET /kategoris
  # GET /kategoris.xml
  def index
    @kategoris = Kategori.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @kategoris }
    end
  end

  # GET /kategoris/1
  # GET /kategoris/1.xml
  def show
    @kategori = Kategori.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @kategori }
    end
  end

  # GET /kategoris/new
  # GET /kategoris/new.xml
  def new
    @kategori = Kategori.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @kategori }
    end
  end

  # GET /kategoris/1/edit
  def edit
    @kategori = Kategori.find(params[:id])
  end

  # POST /kategoris
  # POST /kategoris.xml
  def create
    @kategori = Kategori.new(params[:kategori])

    respond_to do |format|
      if @kategori.save
        format.html { redirect_to(@kategori, :notice => 'Kategori was successfully created.') }
        format.xml  { render :xml => @kategori, :status => :created, :location => @kategori }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @kategori.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /kategoris/1
  # PUT /kategoris/1.xml
  def update
    @kategori = Kategori.find(params[:id])

    respond_to do |format|
      if @kategori.update_attributes(params[:kategori])
        format.html { redirect_to(@kategori, :notice => 'Kategori was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @kategori.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /kategoris/1
  # DELETE /kategoris/1.xml
  def destroy
    @kategori = Kategori.find(params[:id])
    @kategori.destroy

    respond_to do |format|
      format.html { redirect_to(kategoris_url) }
      format.xml  { head :ok }
    end
  end
end

My Admin controller in views/admin/admin_controller.rb

class Admin::AdminController < ApplicationController
def index
end
end

When I visit http://localhost:3000/admin/kategoris I get following error:

ActionController::RoutingError in Admin/kategoris#index

Showing C:/Rails/konkurranceportalen/app/views/admin/kategoris/index.html.erb where line #14 raised:

No route matches {:action=>"show", :controller=>"kategoris", :id=>#<Kategori id: 1, name: "Elektronik", created_at: "2011-02-17 04:18:11", updated_at: "2011-02-17 04:18:11", cached_slug: "">}

Extracted source (around line #14):

11: <% @kategoris.each do |kategori| %>
12:   <tr>
13:     <td><%= kategori.name %></td>
14:     <td><%= link_to 'Show', kategori %></td>
15:     <td><%= link_to 'Edit', edit_kategori_path(kategori) %></td>
16:     <td><%= l

But I am able to create a new on http://localhost:3000/admin/kategoris/new And then I am redirect to http://localhost:3000/kategoris/name-of-new-kategori Instead of http://localhost:3000/admin/kategoris/name-of-new-kategori

The development log:

Started GET "/admin/kategoris" for 127.0.0.1 at 2011-03-03 16:17:12 +0100
  Processing by Admin::KategorisController#index as HTML
  [1m[36mKategori Load (0.0ms)[0m  [1mSELECT `kategoris`.* FROM `kategoris`[0m
Rendered admin/kategoris/index.html.erb within layouts/application (15.0ms)
Completed   in 160ms

ActionView::Template::Error (No route matches {:action=>"show", :controller=>"admin/kategoris", :id=>#<Kategori id: 1, name: "Elektronik", created_at: "2011-02-17 04:18:11", updated_at: "2011-02-17 04:18:11", cached_slug: "">}):
    11: <% @kategoris.each do |kategori| %>
    12:   <tr>
    13:     <td><%= kategori.name %></td>
    14:     <td><%= link_to 'Show', admin_kategori_path(kategori) %></td>
    15:     <td><%= link_to 'Edit', edit_kategori_path(kategori) %></td>
    16:     <td><%= link_to 'Destroy', kategori, :confirm => 'Are you sure?', :method => :delete %></td>
    17:   </tr>
  app/views/admin/kategoris/index.html.erb:14:in `block in _app_views_admin_kategoris_index_html_erb__324114984_51120168__127142517'
  app/views/admin/kategoris/index.html.erb:11:in `each'
  app/views/admin/kategoris/index.html.erb:11:in `_app_views_admin_kategoris_index_html_erb__324114984_51120168__127142517'
  app/controllers/admin/kategoris_controller.rb:7:in `index'

Rendered C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (39.0ms)
Rendered C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (91.0ms)

Rake routes:

>"update", :controller=>"reklamers"}
                        DELETE /reklamers/:id(.:format)                {:action=
>"destroy", :controller=>"reklamers"}
     konkurrencer_index GET    /konkurrencer(.:format)                 {:action=
>"index", :controller=>"konkurrancers"}
                        POST   /konkurrencer(.:format)                 {:action=
>"create", :controller=>"konkurrancers"}
       new_konkurrencer GET    /konkurrencer/new(.:format)             {:action=
>"new", :controller=>"konkurrancers"}
      edit_konkurrencer GET    /konkurrencer/:id/edit(.:format)        {:action=
>"edit", :controller=>"konkurrancers"}
           konkurrencer GET    /konkurrencer/:id(.:format)             {:action=
>"show", :controller=>"konkurrancers"}
                        PUT    /konkurrencer/:id(.:format)             {:action=
>"update", :controller=>"konkurrancers"}
                        DELETE /konkurrencer/:id(.:format)             {:action=
>"destroy", :controller=>"konkurrancers"}
              statistik        /statistik(.:format)                    {:control
ler=>"public", :action=>"statistik"}
        admin_reklamers GET    /admin/reklamers(.:format)              {:action=
>"index", :controller=>"admin/reklamers"}
                        POST   /admin/reklamers(.:format)              {:action=
>"create", :controller=>"admin/reklamers"}
     new_admin_reklamer GET    /admin/reklamers/new(.:format)          {:action=
>"new", :controller=>"admin/reklamers"}
    edit_admin_reklamer GET    /admin/reklamers/:id/edit(.:format)     {:action=
>"edit", :controller=>"admin/reklamers"}
         admin_reklamer GET    /admin/reklamers/:id(.:format)          {:action=
>"show", :controller=>"admin/reklamers"}
                        PUT    /admin/reklamers/:id(.:format)          {:action=
>"update", :controller=>"admin/reklamers"}
                        DELETE /admin/reklamers/:id(.:format)          {:action=
>"destroy", :controller=>"admin/reklamers"}
        admin_kategoris GET    /admin/kategoris(.:format)              {:action=
>"index", :controller=>"admin/kategoris"}
                        POST   /admin/kategoris(.:format)              {:action=
>"create", :controller=>"admin/kategoris"}
     new_admin_kategori GET    /admin/kategoris/new(.:format)          {:action=
>"new", :controller=>"admin/kategoris"}
    edit_admin_kategori GET    /admin/kategoris/:id/edit(.:format)     {:action=
>"edit", :controller=>"admin/kategoris"}
         admin_kategori GET    /admin/kategoris/:id(.:format)          {:action=
>"show", :controller=>"admin/kategoris"}
                        PUT    /admin/kategoris/:id(.:format)          {:action=
>"update", :controller=>"admin/kategoris"}
                        DELETE /admin/kategoris/:id(.:format)          {:action=
>"destroy", :controller=>"admin/kategoris"}
    admin_konkurrancers GET    /admin/konkurrancers(.:format)          {:action=
>"index", :controller=>"admin/konkurrancers"}
                        POST   /admin/konkurrancers(.:format)          {:action=
>"create", :controller=>"admin/konkurrancers"}
 new_admin_konkurrancer GET    /admin/konkurrancers/new(.:format)      {:action=
>"new", :controller=>"admin/konkurrancers"}
edit_admin_konkurrancer GET    /admin/konkurrancers/:id/edit(.:format) {:action=
>"edit", :controller=>"admin/konkurrancers"}
     admin_konkurrancer GET    /admin/konkurrancers/:id(.:format)      {:action=
>"show", :controller=>"admin/konkurrancers"}
                        PUT    /admin/konkurrancers/:id(.:format)      {:action=
>"update", :controller=>"admin/konkurrancers"}
                        DELETE /admin/konkurrancers/:id(.:format)      {:action=
>"destroy", :controller=>"admin/konkurrancers"}
                   root        /(.:format)                             {:control
ler=>"public", :action=>"index"}

C:\Rails\konkurranceportalen>
1
would need to see the output of 'rake routes' as welloliverbarnes
I have posted the routesRails beginner
try namespace :admin do |admin| admin.resources :kategoris end in your routesoliverbarnes
I get a error: Method missing undefined method resourcesRails beginner

1 Answers

1
votes

You'll need to refactor your url helpers to include the admin namespace. In the error's case, it'd be

<td><%= link_to 'Show', admin_kategori_path(kategori) %></td>

Same goes for the redirects in the controller

#in AdminController#create
redirect_to(admin_kategori_path(@kategori))

To see all the generated route helpers, from the console do

rake routes