我使用devise作爲我的用戶身份驗證和載波圖像上傳寶石。現在一切正常,頭像被保存在users表中並顯示在索引視圖中;但不在展示視圖內。阿凡達未向用戶顯示
爲了使我的問題有點更加清晰:
索引視圖中,化身獲取的成功所示。
裏面的表演來看,化身下降到默認的圖像,因爲@user.avatar
是blank/nil
顯示代碼:
<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>
問題控制器:
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
用戶模型:
class User < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
has_many :questions, :dependent => :destroy
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
問題模型:
<% 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 %>