添加带有 simple_form 的复选框而不与模型关联?

发布于 2025-01-03 13:14:48 字数 440 浏览 1 评论 0原文

如何使用 simple_form 添加复选框而不与模型关联? 我想创建一个复选框来处理一些 javascript 事件,但不知道? 也许我错过了文档中的某些内容? 不想使用类似的如下:

= simple_form_for(resource, as: resource_name, url: session_url(resource_name), wrapper: :inline) do |f|
  .inputs
    = f.input :email, required: false, autofocus: true
    = f.input :password, required: false
    = f.input :remember_me, as: :boolean if devise_mapping.rememberable?
    = my_checkbox, 'some text'

How I can add checkbox with simple_form without association with model?
I want to create checkbox which will handle some javascript events, but don't know?
Maybe I miss something in documentation?
Want't to use similar like following:

= simple_form_for(resource, as: resource_name, url: session_url(resource_name), wrapper: :inline) do |f|
  .inputs
    = f.input :email, required: false, autofocus: true
    = f.input :password, required: false
    = f.input :remember_me, as: :boolean if devise_mapping.rememberable?
    = my_checkbox, 'some text'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

千年*琉璃梦 2025-01-10 13:14:48

您可以向模型添加自定义属性:

class Resource < ActiveRecord::Base
  attr_accessor :custom_field
end

然后使用此字段作为块:

= f.input :custom_field, :label => false do 
  = check_box_tag :some_name

尝试在其文档中查找“Wrapping Rails Form Helpers”https://github.com/plataformatec/simple_form

You can add a custom attribute to the model:

class Resource < ActiveRecord::Base
  attr_accessor :custom_field
end

Then use this field as block:

= f.input :custom_field, :label => false do 
  = check_box_tag :some_name

Try to find "Wrapping Rails Form Helpers" in their documentation https://github.com/plataformatec/simple_form

楠木可依 2025-01-10 13:14:48

你可以使用

f.input :field_name, as: :boolean

You could use

f.input :field_name, as: :boolean
浪菊怪哟 2025-01-10 13:14:48

huoxito 提出的命令不起作用(至少在 Rails 4 中不起作用)。据我所知,该错误是由 Rails 尝试查找 :custom_field 的默认值引起的,但由于该字段不存在,因此查找失败并引发异常。

但是,如果您使用 :input_html 参数为字段指定默认值,则它会起作用,例如如下所示:

= f.input :custom_field, :as => :boolean, :input_html => { :checked => "checked" }

The command proposed by huoxito does not work (at least not in Rails 4). As far as I figured out the error is caused by Rails trying to look up the default value for :custom_field, but because this field does not exists this lookup fails and an exception is thrown.

However, it works if you specify a default value for the field using the :input_html parameter, e.g. like this:

= f.input :custom_field, :as => :boolean, :input_html => { :checked => "checked" }
场罚期间 2025-01-10 13:14:48

这个问题首先在谷歌上出现,没有合适的答案。

自 Simple Form 3.1.0.rc1 起,维基百科上就有了一种正确的方法: https://github.com/plataformatec/simple_form/wiki/Create-a-fake-input-that-does-NOT-read-attributes

app/inputs/fake_input.rb

class FakeInput < SimpleForm::Inputs::StringInput
  # This method only create a basic input without reading any value from object
  def input(wrapper_options = nil)
    merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
    template.text_field_tag(attribute_name, nil, merged_input_options)
  end
end

然后你可以做 <%= f.input :thing, as: :fake %>

对于这个特定问题,你必须将方法的第二行更改为:

template.check_box_tag(attribute_name, nil, merged_input_options)

对于 3.1.0 之前的版本.rc1 admgc 给出了一个解决方案,即添加缺少的方法 merge_wrapper_options

https://stackoverflow.com/a/26331237/2055246

This question comes first on google with no appropriate answer.

Since Simple Form 3.1.0.rc1 there is a proper way of doing it explained on the wiki: https://github.com/plataformatec/simple_form/wiki/Create-a-fake-input-that-does-NOT-read-attributes

app/inputs/fake_input.rb:

class FakeInput < SimpleForm::Inputs::StringInput
  # This method only create a basic input without reading any value from object
  def input(wrapper_options = nil)
    merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
    template.text_field_tag(attribute_name, nil, merged_input_options)
  end
end

Then you can do <%= f.input :thing, as: :fake %>

For this specific question you have to change the second line of the method to:

template.check_box_tag(attribute_name, nil, merged_input_options)

For versions prior 3.1.0.rc1 admgc gave a solution that is adding the missing method merge_wrapper_options:

https://stackoverflow.com/a/26331237/2055246

活泼老夫 2025-01-10 13:14:48

将其添加到 app/inputs/atory_boolean_input.rb 中:

class ArbitraryBooleanInput < SimpleForm::Inputs::BooleanInput
  def input(wrapper_options = nil)
    tag_name = "#{@builder.object_name}[#{attribute_name}]"
    template.check_box_tag(tag_name, options['value'] || 1, options['checked'], options)
  end
end

然后在您的视图中使用它,例如:

= simple_form_for(@some_object, remote: true, method: :put) do |f|
  = f.simple_fields_for @some_object.some_nested_object do |nested_f|
    = nested_f.input :some_param, as: :arbitrary_boolean

ie 上述实现正确支持嵌套字段。我见过的其他解决方案没有。

注意:此示例是 HAML。

Add this to app/inputs/arbitrary_boolean_input.rb:

class ArbitraryBooleanInput < SimpleForm::Inputs::BooleanInput
  def input(wrapper_options = nil)
    tag_name = "#{@builder.object_name}[#{attribute_name}]"
    template.check_box_tag(tag_name, options['value'] || 1, options['checked'], options)
  end
end

then use it in your views like:

= simple_form_for(@some_object, remote: true, method: :put) do |f|
  = f.simple_fields_for @some_object.some_nested_object do |nested_f|
    = nested_f.input :some_param, as: :arbitrary_boolean

i.e. The above implementation supports nested fields correctly. Other solutions I've seen do not.

Note: This example is HAML.

苦笑流年记忆 2025-01-10 13:14:48

这是另一种变体:

= f.label :some_param, class: "label__class" do
  = f.input_field :some_param, class: "checkbox__class"
  Label text

Here is another variation:

= f.label :some_param, class: "label__class" do
  = f.input_field :some_param, class: "checkbox__class"
  Label text
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文