Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying Current User in view through a polymorphic association
    primarykey
    data
    text
    <p>I am attempting to display the current users name once they are logged in. </p> <p>Therefore at the top of the page it would say "Logged in as Patrick". However I have a polymorphic association set up whereby every user that signs up is either a player or a coach.</p> <p>The polymorphic association is under the label or :tennis_player as both coach and player play tennis.</p> <p>The code for the view is below.</p> <pre><code> &lt;div class="container"&gt; &lt;header&gt; &lt;div class="logo"&gt; &lt;%= link_to(image_tag 'tennis_ball.png', :width =&gt; 100, :height =&gt; 100) %&gt; &lt;/div&gt; &lt;div class="slogan"&gt; &lt;h3&gt;Setfortennis&lt;/h3&gt; &lt;/div&gt; &lt;div id="user_nav"&gt; &lt;% if current_user? %&gt; Logged in as &lt;%= @current_user %&gt; &lt;%= link_to "log out", log_out_path %&gt; &lt;% else %&gt; &lt;%= link_to "Sign Up", sign_up_path %&gt; or &lt;%= link_to "Log in", log_in_path %&gt; &lt;% end %&gt; &lt;/div&gt; &lt;/header&gt; &lt;/div&gt; </code></pre> <p>Here is my application controller</p> <pre><code> helper_method :current_user? before_filter :get_user def current_user? !!current_user end def current_user @current_user ||= session[:user_id] &amp;&amp; User.find_by_id(session[:user_id]) end def check_logged_in redirect_to( new_session_path, :notice =&gt; "You must be logged in to do that!") unless current_user? end def get_user @user = User.new end end </code></pre> <p>And here are my models. Anything else needed to solve let me know!</p> <pre><code> class Player &lt; ActiveRecord::Base attr_accessible :about, :club, :first_name, :last_name, :profile_picture, :racket, :ranking, :image has_attached_file :image, styles: { thumb: '100x100&gt;', square: '200x200#', medium: '300x300&gt;' } has_many :videos has_one :user, :as =&gt; :tennis_player end class Coach &lt; ActiveRecord::Base attr_accessible :about, :club, :first_name, :last_name, :profile_picture, :ranking has_one :user, :as =&gt; :tennis_player end </code></pre> <p>User Model.</p> <pre><code> class User &lt; ActiveRecord::Base attr_accessible :email, :password_hash, :password_salt, :password, :password_confirmation attr_accessor :password belongs_to :tennis_player, :polymorphic =&gt; true before_save :encrypt_password validates_confirmation_of :password validates_confirmation_of :password, :on =&gt; :create validates_confirmation_of :email validates_uniqueness_of :password def self.authenticate(email, password) user = find_by_email(email) if user &amp;&amp; user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt) user else nil end end def encrypt_password if password.present? self.password_salt = BCrypt::Engine.generate_salt self.password_hash = BCrypt::Engine.hash_secret(password, password_salt) end end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload