ruby on rails - Can I use CanCan with model(s) other than User? -


I'm new to Rail and Construction apps with data on an API, I have two tables that I use for users I want to - students and teachers .

I can authenticate with

  private def fetch_user_data (username, password) in controllers. 'URI' uriy = URI ('URL for API') res = Net :: HTTP.post_form (Yuri, 'username' => username, 'password' = & gt; password) xml = res.body doc = Noteary :: Slop (xml) @status = doc.auth.status.content.to_s @username = doc.auth.username.content.to_s @token = doc.auth.token.content.to_s @person_id = doc.auth. Person_pk.content.to_i @security_roles = doc.auth.security_roles.content.to_s.downcase End DECF Allocate if / faculty / = ~ @security_roles @user = Educator.find_by (person_id: @person_id) elsif / student / = ~ @security_roles @user = Student.find_by (person_id: @person_id) End end df authentication_jeter (user name, password) fetch_user_data (username, password) on assign_user sessi [: user_id] = @ user.id redirect_to @user end  < / Pre> 

Now I know that this is probably not beautiful, but I am learning as I go. I use the authenticate_user () in the session controller, and depending on the redirect, authentication seems okay. I have a question that is the key to the code in the : user_id key - is this the key created for only one session or is it trying to draw a value from the user table? By knowing this will help that my guess is that this is just made for the session, but I do not know.

Okay now for my real problem, I am trying to use Cancans and I am stuck in defining the current user.

I thought I could have @current_user example @user to be assigned in assign_user . It does not seem to work though. I tried some things, but I got stuck. Maybe I can not do that? The big question is? Do I have a user model for Canon working? Can I use two model teachers and students and mask user references in Cancun on them?

I tried to do it, but it is not working - any help?

  def Current_user @current_user || = @user end   

Edit: A little figure. 1. Methods were in ApplicationHelper . Moved from current_user () to ApplicationController Traditionally syntax was changed, but added conditional.

  def current_user if student- (where id: session [: user_id]). Count == 0 @current_user || = Teacher || Address (session [: user_id]) and @ Surrey_User || = Student.Fund (session [: user_id]) End of End   

It starts fixing things, and allows me to use both tables in the form of a user model. There are several ways to define current_user (if you are not using a divis), but

This is a very standard:

  class ApplicationController & lt; ActionController :: Base Def current_user @sert_user || = User.Fund (session [: user_id]) and end   

For your question about the session, the way you have installed it, you have the session equal to [: user_id] @ User.id (which is always the same for every user).

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -