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

时间:2013-08-12 18:55:27

标签: ruby-on-rails ruby ruby-on-rails-3.2

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

1 个答案:

答案 0 :(得分:0)

我认为你所寻找的是浅层筑巢。

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

查找 2.7.2浅嵌套

部分