如何自定义formtastic属性:as=>:check_boxes

发布于 2024-11-26 14:38:50 字数 1237 浏览 1 评论 0原文

我的问题是我尝试自定义格式视图。但在详细介绍之前,我将解释一下我的模型。

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

被翻牌 2024-12-03 14:38:51

尝试覆盖自定义输入:

# app/inputs/collection_check_boxes_input.rb
class CollectionCheckBoxesInput < SimpleForm::Inputs::CollectionCheckBoxesInput
  # [...]
end

Try overriding custom inputs:

# app/inputs/collection_check_boxes_input.rb
class CollectionCheckBoxesInput < SimpleForm::Inputs::CollectionCheckBoxesInput
  # [...]
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文