我一直在摔跤一段時間,並且我的索引頁中的用戶圖片(或默認)沒有顯示。我使用回形針,它可以在除此頁面以外的其他地方工作。用戶的圖片沒有顯示
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
未在索引操作中設置「@ user」變量。 – Nithin 2014-10-10 02:36:38
當我添加'@user = User.find(params [:id])'我得到錯誤_無法找到沒有ID_ – teddybear 2014-10-10 02:41:02
的用戶您怎麼樣在您的應用中授權用戶?任何寶石使用? – Nithin 2014-10-10 02:45:20