Ray
Ray

Reputation: 9684

Avatar is not showing for users

I am using devise as my user authentication and carrierwave gem for image uploading. Now everything works well and the avatar gets saved in the users table and is shown inside the index view; but not inside the show view.

To make my question a bit more clear:

Inside the index view, the avatar get's successfully shown.

enter image description here

Inside the show view, the avatar falls to the default image because @user.avatar is blank/nil

enter image description here

Show code:

  <div class="well">
      <div class="media">
        <a class="pull-left">
        <% if @user.avatar.blank? %>
            <img src="http://www.adtechnology.co.uk/images/UGM-default-user.png" style="width: 75px;">
        <% elsif @user.avatar %>
            <%= image_tag @user.avatar, :style => "width:75px;" %>
        <% end %>
        </a>
        <div class="media-body">
          <p>About <%= link_to @question.user.username, @question.user, :class => " bg" %></p>
       </div>
       <p class="text-muted small">Apparently this user doesn't like to share his information.</p>
    </div>
  </div>

Question controller:

class QuestionsController < ApplicationController
  before_action :set_question, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def index
    @questions = Question.all
    respond_with(@questions)
  end

  def show
    @user = User.find(params[:id])
    respond_with(@question)
  end

  def new
    if user_signed_in? 
      @question = current_user.questions.build
      respond_with(@question)
    else
      redirect_to new_user_session_path
    end
  end

  def edit
  end

  def create
    @question = current_user.questions.build(question_params)
    @question.save
    respond_with(@question)
  end

  def update
    @question.update(question_params)
    respond_with(@question)
  end

  def destroy
    @question.destroy
    respond_with(@question)
  end

  private
    def set_question
      @question = Question.find(params[:id])
    end

    def question_params
      params.require(:question).permit(:title, :description)
    end
end

User model:

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader
  has_many :questions, :dependent => :destroy


  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

Question model:

class Question < ActiveRecord::Base
    belongs_to :user
end

Upvotes: 1

Views: 430

Answers (2)

Harfangk
Harfangk

Reputation: 843

def show
    @user = User.find(params[:id])
    respond_with(@question)
end

Since the show action's called in QuestionsController, params[:id] will be @question's id. You should use @question.user to refer to the author of @question:

def show
    @user = @question.user
    respond_with(@question)
end

Upvotes: 1

Ray
Ray

Reputation: 9684

I fixed it by changing these lines inside the show.html.erb :

<% if @question.user.avatar.blank? %>
    <img src="http://www.adtechnology.co.uk/images/UGM-default-user.png" style="width: 75px;">
<% elsif @question.user.avatar %>
    <%= image_tag @question.user.avatar, :style => "width:75px;" %>
<% end %>

Upvotes: 1

Related Questions