Rails帮助构建管理区域 - 路由问题

时间:2011-03-03 14:13:01

标签: ruby-on-rails ruby-on-rails-3

我正在制作管理区域。

在app / controllers中

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

在app / controllers / admin中

kategoris_controller.rb

我的路线档案:

resources :kategoris
namespace :admin do
  resources :kategoris
end

app / views / admin / kategories / index

中的我的kategori索引文件
<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 %>

我的kategori控制器在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

我的管理员控制器位于views / admin / admin_controller.rb

class Admin::AdminController < ApplicationController
def index
end
end

当我访问http://localhost:3000/admin/kategoris时,我收到以下错误:

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

但是我可以在http://localhost:3000/admin/kategoris/new上创建一个新的 然后我重定向到http://localhost:3000/kategoris/name-of-new-kategori 而不是http://localhost:3000/admin/kategoris/name-of-new-kategori

开发日志:

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)

耙路线:

>"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 个答案:

答案 0 :(得分:1)

您需要重构您的网址助手以包含管理员命名空间。在错误的情况下,它是

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

控制器中的重定向也是如此

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

要查看所有生成的路线助手,请从控制台执行

rake routes