Reputation: 1605
we have lots of panel in out Application like admin , teacher principal , student , parent etc .
Each panel have its own layout So upon login we handle this using WelcomeController
class WelcomeController < ApplicationController
def index
respond_to do |format|
format.html do
return render :home if current_user.nil?
return render :admin if current_user.super?
return redirect_to("/student/lesson") if current_user.student?
return redirect_to("/teacher/lesson") if current_user.teacher?
return render "layouts/principal" if current_user.principal?
return render "layouts/coordinator" if current_user.coordinator?
return render "layouts/viceprincipal" if current_user.viceprincipal?
return render "layouts/parent" if current_user.parent?
end
end
end
end
So right now for getting data from controller we redirect to his route Like for Student
return redirect_to("/student/lesson") if current_user.student?
but we wants that on URL / we get data from controller . So my problem is how to get data ? So we can use in views
I am new to Rails , if I am using something wrong Please let me know . Will I get data from Model ?
In routes we use
get '/student/lesson', to: 'student_lesson_plan#index', as: 'student_lesson'
And from index Action we have variables which we use . So I want instead of
return redirect_to("/student/lesson") if current_user.student?
something like this
return render "layouts/student" if current_user.student?
And I can use those variables which I initialize in student_lesson_plan#index
or from another place
Upvotes: 2
Views: 924
Reputation: 3430
In my application I would set something like this to get different layout depending on conditions:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
layout :layout_by_user_types # works like a before action
def layout_by_user_types
if current_user.student?
"students/application"
elsif current_user.other_condition?
"other_name_space/application"
else
"application"
end
end
end
In my views folder I would separate the different layouts so I can call different css/js if needed ...
-views
-layouts
-students
-_my_partials.html.erb
-application.html.erb
-other_users
-_my_partials.html.erb
-application.html.erb
....
The basic understanding of how we get info from models to views thanks to the controllers:
In a controller
class StudentController < ApplicationController
def_index
@my_var_i_want_in_my_view = Student.my_query_to_database
@my_var_i_want_in_my_view_too = Student.my_super_action_that_will_give_some_data_and_that_is_a_method_in_my_model
end
end
Then in the view you can grab and use @my_var_i_want_in_my_view
Upvotes: 1