Rails Routes based on condition -


i have 3 roles: instuctor, student, admin , each have controllers "home" view.

so works fine,

get "instructor/home", :to => "instructor#home" "student/home", :to => "student#home" "admin/home", :to => "admin#home" 

i want write vanity url below route based on role of user_id correct home page.

get "/:user_id/home", :to => "instructor#home" or "student#home" or "admin#home" 

how accomplish this?

you can't routes because routing system not have information required make decision. rails knows @ point of request parameters , not have access in database.

what need controller method can load whatever data required, presumably user record, , redirects accordingly using redirect_to.

this standard thing do.

update:

to perform of within single controller action need split logic according role. example is:

class homecontroller < applicationcontroller   def home     case     when @user.student?       student_home     when @user.admin?       admin_home     when @user.instructor       instructor_home     else       # unknown user type? render error or use default.     end   end  protected   def instructor_home     # ...     render(:template => 'instructor_home')   end    def student_home     # ...     render(:template => 'student_home')   end    def admin_home     # ...     render(:template => 'admin_home')   end end 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -