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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9182434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:46:27  来源:igfitidea点击:

add checkbox with simple_form without association with model?

ruby-on-railshamlsimple-form

提问by Mikhail Grishko

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 添加复选框?我想创建一个复选框来处理一些 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'

回答by Gacha

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

尝试在他们的文档https://github.com/plataformatec/simple_form 中找到“Wrapping Rails Form Helpers”

回答by huoxito

You could use

你可以用

f.input :field_name, as: :boolean

回答by m4r73n

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.

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

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

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

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

回答by eXa

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

从 Simple Form 3.1.0.rc1 开始,wiki 上解释了正确的方法:https: //github.com/plataformatec/simple_form/wiki/Create-a-fake-input-that-does-NOT-read -属性

app/inputs/fake_input.rb:

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 %>

然后你可以做 <%= 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:

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

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

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

回答by Benjamin Dobell

Add this to app/inputs/arbitrary_boolean_input.rb:

将此添加到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.

注意:此示例是 HAML。

回答by Anton Sergeyev

Here is another variation:

这是另一个变体:

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