I have resources :subjects in my routes which I wants to use as optional routes by using concern.
The idea is to use DRY approach so I do not need to re-write routes lines for other case in which I do not need resources :subjects.
The links are like below and it is clear I have no subject_id in 2nd case as I do not need it:
http://localhost:3000/teacher/katherine-fleming/subjects/3/lesson_plans/3
and
http://localhost:3000/teacher/carmel-cynthia/lesson_plans/3
Right now, it stops working for both cases and saying:
ActionController::RoutingError (No route matches [GET] "/teacher
/katherine-fleming/subjects/3/lesson_plans/3"):
ActionController::RoutingError (No route matches [GET] "/teacher/carmel-
cynthia/lesson_plans/68"):
My routes for case 1 are:
resources :subjects do
concern :lesson_plans, :except => [:index] do
resources :lesson_plan_registrations
resources :comments, only: [:new, :create, :destroy]
end
end
I am trying to make them working using below code but its not working as expected.
resources :lesson_plans, concerns: :lesson_plans
What's working right now is:
resources :subjects do
resources :lesson_plans, :except => [:index] do
resources :lesson_plan_registrations
resources :comments, only: [:new, :create, :destroy]
end
end
resources :lesson_plans, :except => [:index] do
resources :lesson_plan_registrations
resources :comments, only: [:new, :create, :destroy]
end
But its something to re-write the whole code again for the case in which I do not need resources :subjects.
I want to use DRY approach so it will work for both cases.
Here are full routes.rb file:
Rails.application.routes.draw do
root 'welcome#index'
devise_for :users, controllers: {sessions: 'sessions', registrations: 'registrations', :omniauth_callbacks => 'omniauth_callbacks'}
resources :requested_schools, only: [:new, :create]
resources :tags, only: :index
resources :specializations, only: :index
resources :search, only: :index
get '/fetch_schools' => 'search#fetch_schools'
get '/fetch_subjects' => 'search#fetch_subjects'
resources :search_results do
collection do
get '/lesson_plan' => 'search_results#lesson_plan', as: :lesson_plan
get '/video' => 'search_results#video', as: :video
get '/document' => 'search_results#document', as: :document
get '/note' => 'search_results#note', as: :note
end
end
namespace :admin do
root 'dashboard#index'
get 'login' => 'sessions#new', as: :login
resources :dashboard, only: [:index]
resources :requested_schools, only: [:index, :edit, :edit, :show, :destroy] do
member do
get :accept_request
get :decline_request
get 'view_files' => 'requested_schools#view_files', as: :view_files
get 'imported_data' => 'requested_schools#imported_data', as: :imported_data
end
end
resource :users, :except => [:show, :index, :new, :create] do
member do
get :change_password
patch :update_password
end
end
end
namespace :school do
root 'dashboard#index'
resources :dashboard, :path => '/', only: [:index]
resources :departments do
resources :subjects
end
resources :class_rooms do
resources :class_room_subjects
resources :class_room_schedules
resources :class_room_registrations do
collection do
post "/student_registration" => "class_room_registrations#student_registration", as: :student_registration
end
end
end
get '/fetch_subject_teachers' => 'class_room_subjects#fetch_subject_teachers', as: :fetch_subject_teachers
resources :schools, :path => '/', only: [:index, :edit, :show, :destroy, :update] do
collection do
get :change_password
patch :update_password
get :edit_profile
patch :update_profile
get :listing
get :view_ids
match "/upload_ids" => "schools#upload_ids", via: [:get, :post]
get :autocomplete_school_name
get '/view_lesson_plan/:id' => "schools#view_lesson_plan", as: :view_lesson_plan
end
member do
get :students
get :teachers
get :lesson_plans
match '/class_rooms' => "class_rooms#index", via: [:get, :post]
end
end
end
resources :friend_requests do
member do
get '/send_friend_request/:type' => "friend_requests#send_friend_request", as: :send_friend_request
get '/decline_friend_request/:type' => "friend_requests#decline_friend_request", as: :decline_friend_request
get '/accept_friend_request/:type' => "friend_requests#accept_friend_request", as: :accept_friend_request
get '/cancel_friend_request/:type' => "friend_requests#cancel_friend_request", as: :cancel_friend_request
get '/unfriend' => "friend_requests#unfriend", as: :unfriend
end
collection do
get :friend_requests
end
end
namespace :student do
resources :students, :path => '/' do
member do
get :friends
get :lesson_plan_progress
post "/join_private_school/:school_id" => "students#join_private_school", as: :join_private_school
get "/private_schools" => "students#private_schools", as: :private_schools
end
collection do
get :departments
post :select_departments
get :list_departments
get :my_lesson_plan
get :special_lesson_plans
end
get '/view_result/:id' => "students#view_result", as: :view_result
get '/lesson_plan/:id' => "students#lesson_plan", as: :lesson_plan
get '/video/:id' => "students#video", as: :video
post '/video_comments/:id' => "students#video_comments", as: :video_comments
get '/like_video/:id' => "students#like_video", as: :like_video
get '/liked_video_users/:id' => "students#liked_video_users", as: :liked_video_users
get '/is_video_completed' => "students#is_video_completed", as: :is_video_completed
get '/assignment_result/:id' => "students#assignment_result", as: :assignment_result
end
resource :users, :except => [:show, :index, :new, :create] do
member do
get :change_password
patch :update_password
end
end
end
namespace :teacher do
resources :teachers, :path => '/' do
resources :subjects do
resources :lesson_plans, :except => [:index] do
member do
get '/change_public' => "lesson_plans#change_public", as: :change_public
get '/change_private' => "lesson_plans#change_private", as: :change_private
post '/copy_lesson_plan' => "lesson_plans#copy_lesson_plan", as: :copy_lesson_plan
get '/play_list_progress' => "lesson_plans#list_play_list_progress", as: :list_play_list_progress
end
resources :lesson_plan_registrations, :only => [:new, :create, :edit, :update]
post '/move_up_down_play_list' => 'lesson_plans#move_up_down_play_list', as: :move_up_down_play_list
resources :comments, only: [:new, :create, :destroy]
resources :videos do
member do
get '/like_video' => "videos#like_video", as: :like_video
get '/liked_video_users' => "videos#liked_video_users", as: :liked_video_users
get '/delete_video_comment' => "videos#delete_video_comment", as: :delete_video_comment
get '/is_video_completed' => "videos#is_video_completed", as: :is_video_completed
post '/copy_video' => "videos#copy_video", as: :copy_video
end
end
resources :documents do
member do
get '/download_file' => "documents#download_file", as: :download_file
get '/like_document' => "documents#like_document", as: :like_document
get '/liked_document_users' => "documents#liked_document_users", as: :liked_document_users
post '/copy_document' => "documents#copy_document", as: :copy_document
end
end
resources :notes do
member do
get '/like_note' => "notes#like_note", as: :like_note
get '/liked_note_users' => "notes#liked_note_users", as: :liked_note_users
end
end
resources :quizzes do
member do
get '/like_quiz' => "quizzes#like_quiz", as: :like_quiz
get '/liked_quiz_users' => "quizzes#liked_quiz_users", as: :liked_quiz_users
match '/change_public' => "quizzes#change_public", as: :change_public, via: [:get, :post]
get '/give_quiz' => "quizzes#give_quiz", as: :give_quiz
get '/preview_quiz' => "quizzes#preview_quiz", as: :preview_quiz
post '/copy_quiz' => "quizzes#copy_quiz", as: :copy_quiz
end
resources :questions do
collection do
get '/fetch_question_options' => 'questions#fetch_question_options'
match '/search_questions' => 'questions#search_questions', as: :search_questions, via: [:get, :post]
post '/select_questions' => 'questions#select_questions', as: :select_questions
end
end
resources :question_groups
resources :sort_questions, only: [:index]
post '/move_up_down' => 'sort_questions#move_up_down', as: :move_up_down
resources :quiz_answers, only: [:new, :create]
resources :quiz_attempts do
member do
get '/download_file' => "quiz_attempts#download_file", as: :download_file
end
end
end
resources :question_banks do
member do
get :fetch_picks
end
resources :sort_questions, only: [:index]
resources :questions do
member do
post :move
end
collection do
get '/fetch_question_options' => 'questions#fetch_question_options'
end
end
end
resources :assignments do
member do
match '/change_public' => "assignments#change_public", as: :change_public, via: [:get, :post]
get '/like_assignment' => "assignments#like_assignment", as: :like_assignment
get '/liked_assignment_users' => "assignments#liked_assignment_users", as: :liked_assignment_users
get '/download_file' => "assignments#download_file", as: :download_file
post '/copy_assignment' => "assignments#copy_assignment", as: :copy_assignment
end
resources :assignment_submissions do
member do
get '/download_file' => "assignment_submissions#download_file", as: :download_file
post '/mark_assignment' => "assignment_submissions#mark_assignment", as: :mark_assignment
end
end
end
end
end
resources :lesson_plans do
resources :videos
resources :documents
resources :notes
resources :quizzes do
member do
match '/change_public' => "quizzes#change_public", as: :change_public, via: [:get, :post]
get '/preview_quiz' => "quizzes#preview_quiz", as: :preview_quiz
end
resources :questions
resources :quiz_attempts do
member do
get '/download_file' => "quiz_attempts#download_file", as: :download_file
end
end
end
resources :question_banks
resources :assignments
end
resources :class_rooms, :only => [:index] do
resources :subjects do
resources :subject_grades, only: [:index] do
collection do
get "/mark_complete" => "subject_grades#mark_complete", as: :mark_complete
end
end
resources :class_room_subjects, only: [:update]
resources :lessons do
resources :attendances, only: [:index]
end
end
end
resources :private_schools do
resources :invitation_promo_codes
resources :private_classes do
member do
get :new_private_lesson_plan
post :create_private_lesson_plan
get "/edit_private_lesson_plan/:private_lesson_plan_id" => "private_classes#edit_private_lesson_plan", as: :edit_private_lesson_plan
post "/update_private_lesson_plan/:private_lesson_plan_id" => "private_classes#update_private_lesson_plan", as: :update_private_lesson_plan
get "/destroy_private_lesson_plan/:private_lesson_plan_id" => "private_classes#destroy_private_lesson_plan", as: :destroy_private_lesson_plan
end
end
end
member do
get :friends
get '/lesson_plans' => "lesson_plans#index", as: :lesson_plans
get '/my_class_rooms' => "class_rooms#index", as: :my_class_rooms
get :new_promo_code
post :create_promo_code
post :leave_private_school
end
collection do
get :departments
post :select_departments
get '/:teacher_id/invitation_promo_codes' => "invitation_promo_codes#index", as: :invitation_promo_codes
end
end
resource :users, :except => [:show, :index, :new, :create] do
member do
get :change_password
patch :update_password
end
end
end
get '/notifications' => 'users#notifications', as: :notifications
delete '/delete_notification/:id' => 'users#delete_notification', as: :delete_notification
get '/notification_status' => 'users#notification_status'
end
You can try this in your routes:
def lesson_plans
resources :lesson_plans, :except => [:index] do
resources :lesson_plan_registrations
resources :comments, only: [:new, :create, :destroy]
end
end
resources :subjects do
lesson_plans
end
lesson_plans
Better way,
concern :lesson_plans do
resources :lesson_plans, :except => [:index] do
resources :lesson_plan_registrations
resources :comments, only: [:new, :create, :destroy]
end
end
resources :subjects do
concerns :lesson_plans
end
concerns :lesson_plans
I am trying to add a few new pages to a rails resource that I am creating.
What I am doing in my routes file is as follows:
resources :users, :only => [:index, :show] do
collection do
get :show_subpage1
end
end
When I look at my routes, show_subpage1 shows up, but not in the format I want. What shows up in the routes is:
show_subpage1_users GET /users/show_subpage1(.:format)
When what I WANT the route to be is:
show_subpage1_users GET /users/:id/show_subpage1(.:format)
(with the ID).
How would I go about doing that in rails?
To get:
show_subpage1_users GET /users/:id/show_subpage1(.:format)
do not define :show_subpage1 as a collection route:
resources :users, :only => [:index, :show] do
get :show_subpage1
end
or you could define it as a member route as follows:
resources :users, :only => [:index, :show] do
member do
get :show_subpage1
end
end
Also, I'm unsure why you have :only => [:index, :show] defined if you are also going to have a member route :show_subpage1. I assume you do want to add add :show_subpage1 to the only array, i.e. resources :users, :only => [:index, :show, :show_subpage1] do.
Please take a read on "Adding More RESTful Actions"
there are two ways with resources member or collection
resources :users, :only => [ :index, :show ] do
# /users/:id/profile
get 'profile', :on => :member
# /users/profile
get 'profile', :on => :collection
end
hope this helps
try to install active admin , but i encounter this error "Invalid route name, already in use: 'admin_root'"
so after i research stackoverflow . i found some answers ,trying to apply them to my case but it's not working . here's my routes.rb. im confused which routes should i delete to fix the prob.i don't have any admin routes . it's a little confusing.
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
resources :activities, only: [:index, :destroy]
get "relationships/create"
get "relationships/destroy"
get "users/show"
# You can have the root of your site routed with "root"
root 'videos#index'
get 'home', :to => "pages#home", :as => :home
get 'login', :to => "pages#login", :as => :login
get 'about', :to => "pages#about", :as => :about
get 'browse', :to => "pages#browse", :as => :browse
get 'recent', :to => "videos#recent", :as => :recent
devise_for :users
ActiveAdmin.routes(self)
get 'users/:id' => 'users#show', as: :user
resources :relationships, only: [:create, :destroy]
resources :user_friendships
resources :videos
resources :hearts, only: :create
resources :playlists
resources :users do
resources :playlists do
resources :videos
end
member do
get :following, :followers
end
end
You are trying to load the ActiveAdmin routes twice. You have this line twice:
ActiveAdmin.routes(self)
Remove one of those instances and you should be good to go.
I would like to add a url to my comments route so I can call "post_comments_latest_path". I added something like 'get "comments/latest" => "comments#latest", :as => "latest"' but the route adds and the :commend_id to the path that is not needed. Any suggestions?
resources :posts, :except => [:index] do
resources :comments, :except => [:index, :show] do
post "replies" => "comments#create_reply", :as => "create_reply"
get "replies/new" => "comments#new_reply", :as => "new_reply"
end
end
This should work:
resources :posts, :except => [:index] do
resources :comments, :except => [:index, :show] do
post "replies" => "comments#create_reply", :as => "create_rely"
get "replies/new" => "comments#new_reply", :as => "new_reply"
get "latest", :on => "collection"
end
end
A Member route is one that links to a specific resource; requires an id.
A Collection route is one that links to a resource collection; does not require an id.
See the Rails Routing Guide for more information.
So In my rails app I have two resources (rentals, and reservations) which belong to a user. This is the code in my routes.rb to set up the nested routes.
map.resources :users, :has_many => :reservations, :shallow => true
map.resources :users, :has_many => :rentals, :shallow => true
map.resources :rentals, :only => [:index]
map.resources :reservations, :only => [:index]
Is there a more perferred way to do this. I've done some googling but I can't find a clear answer.
Thanks in advance.
-ray
Your method duplicates the routes for users, as you can see by running rake routes. You can fix that by passing a block to map.resources:
map.resources :users, :shallow => true do |user|
user.resources :reservations
user.resources :rentals
end
The nested routes created will assume that you always want to access those resources in a nested fashion.
If you really need all the routes you've defined (including the non-nested rentals and reservations index) then you will need to add:
map.resources :rentals, :only => [:index]
map.resources :reservations, :only => [:index]
And I don't know of a DRYer way to do that.
Nests the two resources under users:
map.resources :users, :shallow => true do |users|
users.resources :reservations, :only => :index
users.resources :rentals, :only => :index
end
Edit: Sorry, forgot the :shallow option.
You can define nested routes with blocks
map.resources :users, :shallow => true do |user|
user.resources :reservations, :only => [:index]
user.resources :rentals, :only => [:index]
end
I feel that this way is a bit more clear and can more easily be adjusted later when you need additional options on one of the nested resources.
The different options and the details are at the ActionController Resources API page