![](/img/trans.png)
[英]Password “can't be blank” when trying to create a new user via ActiveAdmin in Rails 4
[英]Rails can't create new object inside of ActiveAdmin controller
我正在研究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 <%= form_for [@conversation, @message] do |f| %>
當我嘗試調試時,結果發現@message = nil
在_show.html.erb
內。 如果我在 ActiveAdmin controller 中定義@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
通常,您在 controller 中設置實例變量,然后一旦 controller 方法完成,Rails 稍后會隱式渲染視圖。
但是,可以通過在 controller 方法運行時調用諸如render action:
或render template:
之類的東西來顯式渲染視圖,並且可能這發生在對super
的調用中。
有關詳細信息,請參閱布局和渲染 Rails 指南。
您需要將分配移動到調用super
之前。
您可能還需要將@conversation
替換為 ActiveAdmin controller 中的resource
(這是 ActiveAdmin/InheritedResources gem 的東西)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.