有沒有辦法顯示simple_form
視圖中的多態關聯?Simpleform中的多態關聯
到目前爲止,我已經得到了以下:
= simple_form_for(@chat, :html => { :class => "form-horizontal" }, :wrapper => "horizontal", defaults: { :input_html => { class: "form-control"}, label_html: { class: "col-lg-4" } }) do |f|
= f.error_notification
.form-inputs
= f.association :from_user
= f.association :to_user
= f.input :message
= f.association :chattable
.form-actions
= f.button :submit
及以下型號:
class Chat < ActiveRecord::Base
belongs_to :from_user, :foreign_key => 'from_user_id', class_name: 'User'
belongs_to :to_user, :foreign_key => 'to_user_id', class_name: 'User'
belongs_to :chattable, polymorphic: true
validates :from_user, associated: true, presence: true
validates :message, presence: true
end
這將引發如下錯誤:
uninitialized constant Chat::Chattable
這看起來可能是一個包含問題。你能告訴我們聊天模式嗎? – Narfanator
我沒有'chattable'模型。它是'多態的',它是'聊天表'的一部分 –
等待。您的應用名稱是「Chat」還是您的模型?這可能是你的問題。嘗試重命名「聊天」類,然後「set_table_name」聊天「'。 – Narfanator