在我的对话索引页面中,我可以点击其中一个对话,并远程显示对话的消息。当消息出现时,有一个表单可以在两个用户之间的同一对话中发送另一条消息。以下是我设置的方法:
会话/ index.html.haml
- @conversations.each do |conversation|
= link_to conversation_messages_path(conversation), remote: true do
= conversation.topic
.col-lg-12
#messages-index
消息/ _message.html.haml
- @messages.each do |message|
= message.body
= render 'messages/form'
消息/ _form.html.haml
= form_for [@conversation, @message], remote: true do |f|
= f.text_area :body
= f.submit "Send"
消息/ _index.js.erb
$('#messages-index').empty().append('<%= j render @messages %>')
conversations_controller.rb
class ConversationsController < InheritedResources::Base
def index
@conversations = Conversation.all
end
end
messages_controller.rb
class MessagesController < InheritedResources::Base
def index
@conversation = Conversation.find(params[:conversation_id])
@messages = @conversation.messages
@message = current_user.messages.build
respond_to do |format|
format.js
end
end
def create
@message = current_user.messages.build(message_params)
respond_to do |format|
if @message.save
format.js
else
format.js
end
end
end
end
我的问题是当我尝试发送消息并将新消息发布到对话中时:
消息/ _create.js.erb
$('#messages-index').replaceWith('<%= j render partial: "message", locals: {message: @message} %>');
这不起作用,因为它会发送500内部错误:
Started POST "/conversations/6/messages" for 127.0.0.1 at 2015-10-19 04:37:34 -0700
Processing by MessagesController#create as JS
Parameters: {"utf8"=>"✓", "message"=>{"body"=>"dfdfd"}, "commit"=>"Send", "conversation_id"=>"6"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 4]]
(0.2ms) BEGIN
SQL (15.2ms) INSERT INTO "messages" ("body", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["body", "dfdfd"], ["user_id", 4], ["created_at", "2015-10-19 11:37:34.866111"], ["updated_at", "2015-10-19 11:37:34.866111"]]
(47.8ms) COMMIT
Rendered messages/_message.html.haml (5.7ms)
Rendered messages/create.js.erb (7.8ms)
Completed 500 Internal Server Error in 88ms (ActiveRecord: 63.6ms)
NoMethodError - undefined method `each' for nil:NilClass:
app/views/messages/_message.html.haml:1:in `_app_views_messages__message_html_haml___2837667999247155781_70076471170800'
actionview (4.2.3) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.3) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.3) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.3) lib/action_view/template.rb:143:in `render'
........
那么我怎么能以正确的方式添加它,以免我发现each
错误?
更新
所以我想我应该如何让@messages
超出局部。现在怎么办?这就是我的目标:
#messages-index
- @messages.each do |message|
= render message
更新2
因此,我成功解决了接收邮件nil
和each
的问题。我必须做的是将conversation_id
分配给邮件,因为它没有发生,但仍然无法正确附加。我开始认为你无法通过这种方式设置它。
答案 0 :(得分:1)
你像这样呈现部分:
render partial: "message", locals: {message: @message}
render @messages
但在部分_message.html.haml
内,您使用变量@messages
。根据rails惯例,您应该在`_message.html.haml&#39;内使用@message
。
请注意,放置= render 'messages/form'
并不是一件好事,因为这部分内容适用于单个邮件呈现。
<强>更新强>
这就是我的意思:
会话/ index.html.haml
- @conversations.each do |conversation|
= link_to conversation_messages_path(conversation), remote: true do
= conversation.topic
.col-lg-12
#messages-index
.col-lg-12
#new-message-form
消息/ _message.html.haml
= @message.body
消息/ _form.html.haml
= form_tag conversation_messages_path(@conversation), remote: true do
= text_area_tag :body
= submit_tag "Send"
消息/ _index.js.erb
# Render '_message' partial for each message from @messages
$('#messages-index').empty().append('<%= j render @messages %>')
# Render '_form' partial for new message for @conversation
$('#new-message-form').empty().append('<%= j render partial: "form", locals: { conversation: @conversation }) %>')
答案 1 :(得分:0)
当@messages
为nil
时,会显示错误。只需在app/views/messages/_message.html.haml
中添加条件即可检查@messages.nil?