有一个超级快的问题。我还是 Rails 的新手,并尝试遵循这两个问题,但它们对我不起作用:为什么 Array.to_s 返回括号?和ruby 1.9 如何将数组转换为没有括号的字符串。
我正在尝试在我的聊天室应用程序中显示最后一条消息及其发送日期。我能够使用此代码获得结果,但它周围有括号,我想删除这些括号。这里的任何帮助都会很棒,我还附上了截图。非常感谢!
显示.html.erb
For the Date: <%= chatroom.messages.last(1).pluck(:created_at) %> For the Last Message in Chatroom: <%= chatroom.messages.last(1).pluck(:body) %>
直接消息控制器
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
试试这个:
<%= chatroom.messages.last.created_at %>
和这个:
<%= chatroom.messages.last.body %>
请记住,pluck返回一个数组,因此可以解释您的括号。
我认为您不需要pluck
这里,因为您只是访问单个项目的属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句