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
Given the following routes file:
Rails.application.routes.draw do
root to: 'visitors#index'
devise_for :users
resources :users do
resources :wishlists, :only => [:create] do
post :action => :create, :on => :collection
resources :items, :only => [:create, :update, :remove_item] do
post :action => :create, :on => :collection
put :action => :update
delete :action => :remove_item
end
end
end
end
Rails generates routes including the following routes which conflict:
PUT /wishlists/:wishlist_id/items/:item_id(.:format) items#update
wishlist_item PUT /wishlists/:wishlist_id/items/:id(.:format) items#update
Why does the first of these get generated? I would expect only the second one (which includes the path helper)
I'm using Rails 4.1.4
Because your are declaring 2 times the same route:
the first one in resources :items, :only => [:create, :update, :remove_item] generates this resource: /wishlists/:wishlist_id/items/:id(.:format)
the second in put :action => :update generates this one: /wishlists/:wishlist_id/items/:item_id(.:format)
You should be using only 1 of them (I recommend the first one).
If you want more information on routing, you should definitely go on this page.
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
My devise set up was working fine before, but now, for some reason, whenever I try to sign up a new user, it tries to call users#create instead of registrations#create. I think it must be a problem with my routes.rb file. I recently added a new resource, "preferences", to my application, so the routing might be wonky:
Indexer2::Application.routes.draw do
resources :preferences
get "home/index"
resources :posts
resources :users
devise_for :users, :controllers => {:registrations => 'registrations', :invitations => 'invitations'}, :except => [:show] do
get "/signup" => "devise/registrations#new", :as => 'user_signup'
get '/logout' => 'devise/sessions#destroy', :as => 'user_logout'
get '/login' => "devise/sessions#new", :as => 'user_login'
end
match '/welcome' => 'pages#welcome'
resources :preferences, :except => [:destory, :edit, :create, :new, :index, :show] do
collection do
post "make_feed_preference"
post "change_preference"
end
end
root :to => "home#index"
end
Your UsersController should have create method.
If you don't want to write your own registration logic just do inheritance from Devise::RegistrationsController < DeviseController:
controller UsersController < Devise::RegistrationsController
#....
end
This will include default Devise methods.
My login was working fine but I don't know where I messed things up. When I login it redirects me sessions/user which is wrong. Here is the error:
No route matches [POST] "/sessions/user"
Here is my routes.rb:
Wal::Application.routes.draw do
resources :sessions, :only => [:new, :create, :destroy]
devise_for :users, :skip => [:sessions]
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
resources :posts do
resources :comments
end
resources :users, :only => [:show]
resources :microsposts, :only => [:create, :destroy]
match '/' => "home#index", :as => :home
match 'home/index', :to => 'home#index'
get "users/show"
root :to => "home#index"
#get "home/index"
resources :users do
member do
get :following, :followers
end
end
resources :microsposts, :only => [:create, :destroy]
resources :relationships, :only => [:create, :destroy]
application controller:
class ApplicationController < ActionController::Base
protect_from_forgery
def after_sign_in_path_for(resource)
current_user # <- Path you want to redirect the user to after signup
end
def after_sign_up_path_for(resource)
current_user
end
Try something like this
class ApplicationController < ActionController::Base
def after_sign_in_path_for(resource)
stored_location_for(resource) || welcome_path
end
end
https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in-out
Don't know if this will solve your problem, but the first line:
Wal::Application.routes.draw do
has no corresponding 'end' in the code that you posted.