2014-10-10 3 views
0

Я боролся с этим некоторое время, и изображения пользователей (или по умолчанию) на моей индексной странице не отображаются. Я использую paperclip, и он работает везде, кроме этой страницы.Изображения пользователей не отображаются

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 

Update

разговор модель

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

Переменная '@ user' не установлена ​​в действии индекса. – Nithin

+0

Когда я добавляю '@user = User.find (params [: id])' Я получаю ошибку _Couldn't найти пользователя без ID_ – teddybear

+0

как вы, как вы разрешаете пользователям в вашем приложении? любой драгоценный камень используется? – Nithin

ответ

0

Я получил его. Это было фактически скрыто в вспомогательном методе

<%= image_tag conversation_interlocutor(conversation).avatar(:thumb), class: "media-object pull-left" %> 
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" %> 
+0

Я понял это. Спасибо за помощь. – teddybear

Смежные вопросы