私のアプリは基本的に、ユーザーがコースを受講し、コースを完了するためにさまざまな手順を実行することで構成されています。ただし、ルーティングに問題があると思います。今ここにコース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