【发布时间】:2017-08-17 00:48:11
【问题描述】:
有一个超级快速的问题。我仍然是 Rails 新手,并尝试关注这两个问题,但它们对我不起作用:Why does Array.to_s return brackets? 和 ruby 1.9 how to convert array to string without brackets。
我正在尝试在我的聊天室应用程序中显示最后一条消息及其发送日期。我可以使用此代码获得结果,但它周围有括号,我想删除这些括号。这里的任何帮助都会很棒,我还附上了截图。非常感谢!
Show.html.erb
For the Date:
<%= chatroom.messages.last(1).pluck(:created_at) %>
For the Last Message in Chatroom:
<%= chatroom.messages.last(1).pluck(:body) %>
DirectMessages 控制器
class DirectMessagesController < ApplicationController
before_action :authenticate_user!
def show
users = [current_user, User.find(params[:id])]
@messageduser = User.find(params[:id])
@chatroom = Chatroom.direct_message_for_users(users)
@chatroomall = current_user.chatrooms
@messages = @chatroom.messages.order(created_at: :desc).limit(100).reverse
@messagelast = @chatroom.messages.last(1)
last_message = @chatroom.messages.last
render "chatrooms/show"
end
private
def chatroomuserlocator
@chatroomlocator = Chatroom.find(params[:chatroom_id])
end
end
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-5