我已经和这个用户进行过一段时间的摔跤了。我的索引页面中的图像(或默认值)不显示。我使用了回形针,除了这个页面,它可以在其他任何地方使用。
user.rb
has_attached_file :avatar, styles: { larger: "280x280#", medium: "300x300#", thumb: "50x50#", followp: "208x208#" },
default_url: "/assets/default.png",
:url => "/assets/products/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/products/:id/:style/:basename.:extension"
user_controller.rb
def index
#@user = User.find(params[:id]) **This causes the error "couldn't find the user w/o an id"**
@users = User.where.not("id = ?",current_user.id).order("created_at DESC")
@conversations = Conversation.involving(current_user).order("created_at DESC")
end
index.html.erb
<div id="content-box">
<h1>Chat</h1>
<div>
<% if @conversations.any? %>
<ul class="media-list">
<% @conversations.each do |conversation| %>
<li class="media">
<%= image_tag @user.avatar(:thumb), class: "media-object pull-left" unless @user.nil? **removing this causes "undefined method avatar for nil:NilClass"** %>
conversation_controller.rb
def create
if Conversation.between(params[:sender_id],params[:recipient_id]).present?
@conversation = Conversation.between(params[:sender_id],params[:recipient_id]).first
else
@conversation = Conversation.create!(conversation_params)
end
render json: { conversation_id: @conversation.id }
end
def show
@user = User.find(params[:id])
@conversation = Conversation.find(params[:id])
@reciever = interlocutor(@conversation)
@messages = @conversation.messages
@message = Message.new
end
更新
对话模型class Conversation < ActiveRecord::Base
belongs_to :sender, :foreign_key => :sender_id, class_name: 'User'
belongs_to :recipient, :foreign_key => :recipient_id, class_name: 'User'
has_many :messages, dependent: :destroy
validates_uniqueness_of :sender_id, :scope => :recipient_id
scope :involving, -> (user) do
where("conversations.sender_id =? OR conversations.recipient_id =?",user.id,user.id)
end
scope :between, -> (sender_id,recipient_id) do
where("(conversations.sender_id = ? AND conversations.recipient_id =?) OR (conversations.sender_id = ? AND conversations.recipient_id =?)", sender_id,recipient_id, recipient_id, sender_id)
end
end
User.rb
has_many :conversations, :foreign_key => :sender_id
答案 0 :(得分:2)
试试这个:
<div id="content-box">
<h1>Chat</h1>
<div>
<% if @conversations.any? %>
<ul class="media-list">
<% @conversations.each do |conversation| %>
<li class="media">
<%= image_tag User.find_by_id(conversation.sender_id).avatar(:thumb), class: "media-object pull-left" %>
答案 1 :(得分:0)
我明白了。它实际上隐藏在辅助方法
中<%= image_tag conversation_interlocutor(conversation).avatar(:thumb), class: "media-object pull-left" %>