如何自定义formtastic属性:as=>:check_boxes
我的问题是我尝试自定义格式视图。但在详细介绍之前,我将解释一下我的模型。
我有 2 个具有 an:m 关系的对象 Shop 和 Category
Shop 模型看起来像这样:
has_many :shop_categories, :class_name => "ShopCategory", :foreign_key => "shop_id"
has_many :categories, :through => :shop_categories, :source => :categories
Category 模型看起来像这样:
has_many :shop_categories, :class_name => "ShopCategory", :foreign_key => "category_id"
has_many :shops, :through => :shop_categories, :source => :shops
当然我的 m 到 n 表看起来像这样
belongs_to :shops, :class_name => "Shop", :foreign_key => "shop_id"
belongs_to :categories, :class_name => "Category", :foreign_key => "category_id"
validates :shop_id, :presence => true
validates :category_id, :presence => true
这工作正常,我的 Shop 视图中的以下命令将列出来自的所有元素复选框内的类别:
<%= f.input :categories, :as => :check_boxes, :id => 'shop_categories' %>
这是我的问题: 在类别中,我有类别的名称和图片。现在我想在选择框旁边显示图片。
我还尝试使用 <% f.fields_for :categories do |category| %>
但 Rails 不会遍历所有类别元素。
有没有办法通过自动检查对象来处理所有类别元素?
如果您需要更多信息,我将很乐意提供您理解问题所需的所有信息。 谢谢你的任何提示。
my problem is that I try to customize the formatastic view. But before I go into detail, I'll explain my model.
I have 2 objects with a n:m relation Shop and Category
Shop model looks like that:
has_many :shop_categories, :class_name => "ShopCategory", :foreign_key => "shop_id"
has_many :categories, :through => :shop_categories, :source => :categories
Category model looks like that:
has_many :shop_categories, :class_name => "ShopCategory", :foreign_key => "category_id"
has_many :shops, :through => :shop_categories, :source => :shops
And of course my m to n table looks like
belongs_to :shops, :class_name => "Shop", :foreign_key => "shop_id"
belongs_to :categories, :class_name => "Category", :foreign_key => "category_id"
validates :shop_id, :presence => true
validates :category_id, :presence => true
This works fine and the following command in my Shop view will list all elements from categories within checkboxes:
<%= f.input :categories, :as => :check_boxes, :id => 'shop_categories' %>
Here is my problem:
Within categories I have a name for the category and a picture. Now I want to display the picture next to the selectbox.
I also tryed to use <% f.fields_for :categories do |category| %>
but rails wont go through all category elements.
Is there a way to handle all Category elements with automatic checked objects?
If you need more information, i will be glad to give all what you need to understand the problem.
Thank you for any hint.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试覆盖自定义输入:
Try overriding custom inputs: