0

我的应用程序基本上由一个用户参加课程,并通过各种步骤来完成课程。但是,我认为我的路由存在问题。现在这里是路线 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

我将如何实现这一点,路由是否有意义?

路由.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
4

1 回答 1

0

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

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

查找第2.7.2 节浅嵌套

于 2013-08-12T18:57:25.447 回答