【发布时间】:2021-06-03 11:18:14
【问题描述】:
我正在处理User 和AdminUser 之间的消息传递系统。 User 部分现在已准备就绪我正在努力如何允许Admin 发送对由 ActiveAdmin 内部的User 发起的对话的回复。
代码如下:
# app/admin/conversations.rb
ActiveAdmin.register Conversation do
decorate_with ConversationDecorator
# ...
controller do
def show
super
@message = @conversation.messages.build
end
end
end
app/views/admin/conversations/_show.html.erb
# ...
<%= form_for [@conversation, @message] do |f| %>
<%= f.text_area :body %>
<%= f.text_field :messageable_id, value: current_user.id, type: "hidden" %>
<%= f.text_field :messageable_type, value: "#{current_user.class.name}", type: "hidden" %>
<%= f.submit "Send Reply" %>
<% end %>
<% end %>
这给了我一个错误:
表单中的第一个参数不能包含 nil 或为空 提取的源代码(在 #51 行附近): 51
当我尝试调试时,发现 @message = nil 在 _show.html.erb 内部。如果我在 ActiveAdmin 控制器中定义 @message 怎么可能?
[编辑]
如果你好奇,下面的 ConversationController:
class ConversationsController < ApplicationController
before_action :authenticate_user!
def index
@admins = AdminUser.all
@conversations = Conversation.all
end
def new
@conversation = Conversation.new
@conversation.messages.build
end
def create
@conversation = Conversation.create!(conversation_params)
redirect_to conversation_messages_path(@conversation)
end
end
#routes
resources :conversations do
resources :messages
end
【问题讨论】: