路由当前需要模型属性的文本id,而不是在给定上下文的情况下有意义的id



我的应用程序基本上包括一个用户参加课程,并通过各种步骤完成课程。然而,我认为我的路线有问题。现在这是课程1>级别1>步骤1 的路线

 http://localhost:3000/courses/1/levels/1/steps/1    

这是课程2>级别1(当然是2)>步骤1(当然也是2)的路线:

 http://localhost:3000/courses/2/levels/4/steps/10

它需要步骤和级别的文字ID。事实上,我认为如果上面的路线说:会更有意义

 http://localhost:3000/courses/2/levels/1/steps/1

甚至

http://localhost:3000/course_title/levels/1/steps/1

我将如何实现这一点,路由是否合理?

Routes.rb

Serenity::Application.routes.draw do

  root to: 'static_pages#home' 

  match '/help', to: 'static_pages#help'
  match '/about', to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'
  devise_for :admin_users, ActiveAdmin::Devise.config
  ActiveAdmin.routes(self)
  devise_for :users
  ActiveAdmin.routes(self)
  resources :users do
    member do
      get :courses
    end
  end
  resources :courses do
    resources :levels, only: [:show] do
      resources :steps, only: [:show]
    end
  end
  resources :assignments, only: [:create, :destroy]
end

简而言之,以下是我的模型:用户通过分配模型学习课程,每个课程都有级别和步骤。

class User < ActiveRecord::Base
  has_many :assignments, dependent: :destroy
  has_many :courses, through: :assignments
class Assignment < ActiveRecord::Base
  attr_accessible :course_id
  belongs_to :user
  belongs_to :course
class Course < ActiveRecord::Base
  has_many :assignments
  has_many :users, through: :assignments
  has_many :levels
class Level < ActiveRecord::Base
  belongs_to :course
class Step < ActiveRecord::Base
  belongs_to :level

我认为您正在寻找的是浅嵌套。

http://edgeguides.rubyonrails.org/routing.html

查找章节2.7.2浅嵌套

最新更新