使用非restful操作路由发出rails

时间:2014-07-12 16:34:23

标签: ruby-on-rails ruby model-view-controller ruby-on-rails-4 routing

我遇到了rails的路由问题。我会尽我所能解释。

用户可以使用条目控制器的创建和索引操作。

使用devise进行身份验证,管理员可以在条目控制器上执行其余的其余操作。此外,还有三个页面我想添加,“待定,批准,拒绝”,所以我将这些添加到我的条目控制器中,如下所示:

class EntriesController < ApplicationController
  before_action :get_entries, only: [:index, :pending, :approved, :rejected]

  expose(:entry){@entry}
  expose(:entries){@entries}

    def create
    @entry = Photo.new(params_entry)
    if @entry.save
        record_saved
      return redirect_to(entries_path)
    else
        check_for_errors
      return render('new')
    end 
  end

  def index
  end

  def new
    @entry = Photo.new
  end


  def show
  end

  def pending 
  end

  def approved 
  end

  def rejected
  end

  private 

  def params_photo
    params.require(:photo).permit!
  end

  def get_photos
    @entries = Entry.all
  end

end

(注意,这个控制器不完整,但出于我的问题的目的,它就足够了)

我正在尝试配置路由,以便查看这些页面的路径就像

http://localhost:3000/admin/entries/pending

所以我已经配置了我的路线:

  devise_for :admins  # The priority is based upon order of creation: first created -> highest priority.
  root 'home#index'
  resources :entries, :only => [:index, :new]

  namespace :admin do 
    resources :entries do
      collection do
        match :pending, :via => [:get, :post]
        match :rejected, :via => [:get, :post]
        match :approved, :via => [:get, :post]
      end
    end
  end

除了我收到错误

uninitialized constant Admin::EntriesController

期待如果我将'admin'更改为复数'admins'并相应地配置我的视图路径,我会收到此错误:

No route matches [GET] "/admin/entries/pending"

为什么会这样?我正确使用'命名空间'和'集合'。我该如何解决这个问题?

rake routes:

                   Prefix Verb     URI Pattern                       Controller#Action
        new_admin_session GET      /admins/sign_in(.:format)         devise/sessions#new
            admin_session POST     /admins/sign_in(.:format)         devise/sessions#create
    destroy_admin_session DELETE   /admins/sign_out(.:format)        devise/sessions#destroy
           admin_password POST     /admins/password(.:format)        devise/passwords#create
       new_admin_password GET      /admins/password/new(.:format)    devise/passwords#new
      edit_admin_password GET      /admins/password/edit(.:format)   devise/passwords#edit
                          PATCH    /admins/password(.:format)        devise/passwords#update
                          PUT      /admins/password(.:format)        devise/passwords#update
cancel_admin_registration GET      /admins/cancel(.:format)          devise/registrations#cancel
       admin_registration POST     /admins(.:format)                 devise/registrations#create
   new_admin_registration GET      /admins/sign_up(.:format)         devise/registrations#new
  edit_admin_registration GET      /admins/edit(.:format)            devise/registrations#edit
                          PATCH    /admins(.:format)                 devise/registrations#update
                          PUT      /admins(.:format)                 devise/registrations#update
                          DELETE   /admins(.:format)                 devise/registrations#destroy
                     root GET      /                                 home#index
                  entries GET      /entries(.:format)                entries#index
                new_entry GET      /entries/new(.:format)            entries#new
    pending_admin_entries GET|POST /admin/entries/pending(.:format)  admin/entries#pending
   rejected_admin_entries GET|POST /admin/entries/rejected(.:format) admin/entries#rejected
   approved_admin_entries GET|POST /admin/entries/approved(.:format) admin/entries#approved
            admin_entries GET      /admin/entries(.:format)          admin/entries#index
                          POST     /admin/entries(.:format)          admin/entries#create
          new_admin_entry GET      /admin/entries/new(.:format)      admin/entries#new
         edit_admin_entry GET      /admin/entries/:id/edit(.:format) admin/entries#edit
              admin_entry GET      /admin/entries/:id(.:format)      admin/entries#show
                          PATCH    /admin/entries/:id(.:format)      admin/entries#update
                          PUT      /admin/entries/:id(.:format)      admin/entries#update
                          DELETE   /admin/entries/:id(.:format)      admin/entries#destroy

文件夹结构

controllers > admin_controller.rb 
              admin > entries_controller.rb (this is the one with pending etc)
              entries_controller.rb (this is the public one with index etc)

views > admins >  index.html.erb 
                  entries > pending.html.erb, rejected.html.erb etc 

由于

0 个答案:

没有答案