How to use concerns in routes ruby on rails - ruby-on-rails

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

Related

How to create this route?

I have the following route:
resources :articles, except: [:index] do
collection do
get 'author/:id/articles' => 'articles#index', as: 'index_articles'
end
end
This results in:
GET /api/v1/articles/:id/author/:id/articles(.:format)
How can I turn this into the following?:
GET /api/v1/author/:id/articles(.:format)
# config/routes.rb
resources :articles, except: [:index]
resources :authors, path: "author", only: [] do
resources :articles, only: :index, on: :member #-> url.com/author/:id/articles
end
Write this
get 'author/:id/articles' => 'articles#index', as: 'index_articles'
Outside the resources :articles block.

Routes for create action

I have my user_controller and the action create. I do however want to set a custom route to the create action so when the user hits register the create action sets the url to user/thank-you. I have added the following match to my routes file but the url stays unchanged after hitting the register button. Here is part of my routes file until the line with thank-you.
get '/:locale' => 'pages#home'
root :to => 'pages#home'
scope "(:locale)", locale: /en|es/ do
get "javascripts/dynamic_cities"
get "javascripts/dynamic_cities_to_stadia"
get "javascripts/dynamic_stadia"
get "javascripts/dynamic_modality_to_max_players"
get "tournaments/edit_info"
get "tournaments/seasons"
get "league_rosters/cancel_new"
get "tournament_administrations/cancel_new"
resources :admin_tasks, :only => [:index]
resources :sports
resources :countries
resources :cities
resources :stadia
resources :users
resources :sessions, :only => [:new, :create, :destroy]
resources :teams
resources :tournaments
resources :leagues
resources :brackets, :only => [:new, :create, :edit, :update, :show]
resources :labs, :only => [:show, :edit]
resources :seasons, :only => [:edit, :show, :destroy]
resources :matches, :only => [:show, :edit, :update]
resources :player_participations
resources :highlights
resources :users do
resources :requests, :name_prefix => "user_"
end
# match 'league/:id/subscription' => 'league_rosters#new', :as => :subscription
match '/contact', :to => 'pages#contact'
match '/terms_and_conditions', :to => 'pages#terms_and_conditions'
match '/about_us', :to => 'pages#about_us'
match '/cookie_excluder', :to => 'pages#cookie_excluder'
match '/vacancies', :to => 'pages#vacancies'
match '/signup', :to => 'users#new'
match '/thank-you', :to => 'users#create'
Here is also the create method in the users_controller.rb
def create
# Note: This function is repeated in request controller in the invitations part. So any change should be added to request controller aswell
#user = User.new(email: params[:user][:email].downcase,
name: params[:user][:name].capitalize,
password: params[:user][:password],
password_confirmation: params[:user][:password_confirmation],
language: params[:user][:language])
#for user_menu
#title = t("user.new.title")
if #user.save
confirmation_code = "#{#user.id}#{random_string}"
if #user.update_attributes(confirmation_code: confirmation_code)
UserMailer.welcome_email(#user).deliver
vars = Hash.new
vars[:cc] = "#{confirmation_code}"
confirmation_url = url_maker(params[:request_protocol], params[:request_host_with_port], params[:request_locale], "#{email_confirmation_path}", vars)
UserMailer.confirm_email(#user, confirmation_url).deliver
sign_in #user
#user_tournaments = current_user.tournaments.all(:order => "name ASC")
#user_teams = current_user.teams.all(:order => "name ASC")
else
render 'new'
end
else
render 'new'
end
end
Is it possible for this to work the way I am approaching it or I will have to make a redirect and a thank-you page, which I can easily manage through my pages controller?
Move your thank-you route above the resources block as follows:
...
get "league_rosters/cancel_new"
get "tournament_administrations/cancel_new"
# Move route here
match '/thank-you', :to => 'users#create', via: [:post]
resources :admin_tasks, :only => [:index]
resources :sports
...
I'm also adding via: [:post] to the route since:
Routing both GET and POST requests to a single action has security implications. In general, you should avoid routing all verbs to an action unless you have a good reason to.
[http://guides.rubyonrails.org/routing.html]

Issue with configuring active admin

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.

resource available output parent resource rails 3.2

I have this in my routes.rb
resources :users, :path => "/", :only => [:show] do
resources :collections, :controller => 'users/collections'
end
Why can I access to collections resources from:
http://localhost:3000/en/collections
if this resouce this inside users?
I want that collection resource only is enable inside:
http://localhost:3000/en/users/collections
This is my routes:
user_collections GET (/:locale)/:user_id/collections(.:format) users/collections#index
POST (/:locale)/:user_id/collections(.:format) users/collections#create
new_user_collection GET (/:locale)/:user_id/collections/new(.:format) users/collections#new
edit_user_collection GET (/:locale)/:user_id/collections/:id/edit(.:format) users/collections#edit
user_collection GET (/:locale)/:user_id/collections/:id(.:format) users/collections#show
PUT (/:locale)/:user_id/collections/:id(.:format) users/collections#update
DELETE (/:locale)/:user_id/collections/:id(.:format) users/collections#destroy
How can I do it?
Thank you
Try this:
resources :users, :only => [:show] do
resources :collections, :controller => 'users/collections'
end

Rails 3.1 Devise all of a sudden redirecting to wrong path

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.

Resources