如何编写自定义 SimpleForm Builder 来替换通过

发布于 2024-12-04 17:07:08 字数 1927 浏览 1 评论 0原文

我的 Gemfile 摘录:

gem 'rails', '3.0.3'
gem 'inherited_resources', '1.2.1'
gem 'simple_form', '1.4.0'

对于任何资源,我都有 1 个视图来执行 3 个操作(新建、编辑和显示)。示例:

<h1><%= I18n.t('admin.form.'+action_name.downcase, :name => controller_friendly_name) %></h1>
<%= simple_form_for([:admin, resource]) do |f| %>
  <%= render "admin/shared/errors" %>
  <%= f.input :title, 
  :label => "Title", 
  :hint => I18n.t('admin.form.input.title.hint', :name => controller_friendly_name), 
  :required => true,
  :error => false,
  :input_html => { :class => :large, :placeholder => I18n.t('admin.form.input.title.placeholder', :name => controller_friendly_name) }
  %>
  <%= f.input :is_visible, 
  :as => :radio, 
  :label => "Visible", 
  :error => false, 
  :required => true, 
  :collection => [['Yes', true], ['No', false]],
  :wrapper_class => 'checkboxes-and-radiobuttons', 
  :checked => true
  %>
  <%= render "admin/shared/validation", :f => f %>
<% end %>

<% init_javascript "MyApplication.Form.disable();" if [:show].include?(action_name.to_sym) %>

看看 #show 操作如何将所有字段设置为禁用?这太丑了。
考虑到我无法重构视图以拥有 show.html.erb 文件。

我想要做什么:

当操作是 #show 时,simple_form 构建器使用自定义构建器来替换

此外,我将自定义单选按钮、复选框。

我的第一步:

# app/inputs/showvalue_input.rb
class ShowvalueInput < SimpleForm::Inputs::Base
  def input
    # how to change 'text_field' by <p> container ?
    @builder.text_field(attribute_name, input_html_options)
  end
end

找不到方法。自定义表单生成器或自定义输入(带有猴子修补)?

感谢您的帮助!

Extract of my Gemfile:

gem 'rails', '3.0.3'
gem 'inherited_resources', '1.2.1'
gem 'simple_form', '1.4.0'

For any resource, I have 1 view for the 3 actions (new, edit & show). Example:

<h1><%= I18n.t('admin.form.'+action_name.downcase, :name => controller_friendly_name) %></h1>
<%= simple_form_for([:admin, resource]) do |f| %>
  <%= render "admin/shared/errors" %>
  <%= f.input :title, 
  :label => "Title", 
  :hint => I18n.t('admin.form.input.title.hint', :name => controller_friendly_name), 
  :required => true,
  :error => false,
  :input_html => { :class => :large, :placeholder => I18n.t('admin.form.input.title.placeholder', :name => controller_friendly_name) }
  %>
  <%= f.input :is_visible, 
  :as => :radio, 
  :label => "Visible", 
  :error => false, 
  :required => true, 
  :collection => [['Yes', true], ['No', false]],
  :wrapper_class => 'checkboxes-and-radiobuttons', 
  :checked => true
  %>
  <%= render "admin/shared/validation", :f => f %>
<% end %>

<% init_javascript "MyApplication.Form.disable();" if [:show].include?(action_name.to_sym) %>

See how the #show action set all the fields to disabled ? This is ugly.
Consider I can't refactor the views to have a show.html.erb file.

What I want to do:

When the action is #show, the simple_form builder use a custom builder wich replace <input>, <textarea>, <select> by <p> html tag, with the value.

Furthermore, I will customise the radiobuttons, checkboxes to.

My first step:

# app/inputs/showvalue_input.rb
class ShowvalueInput < SimpleForm::Inputs::Base
  def input
    # how to change 'text_field' by <p> container ?
    @builder.text_field(attribute_name, input_html_options)
  end
end

Can't find the way to do it. Custom Form Builders or Custom Inputs (with monkey patching) ?

Thank for the help !

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

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

发布评论

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

评论(1

咽泪装欢 2024-12-11 17:07:08

这是我的

application_helper.rb 中的解决方案:

    def set_show_method_to_builder(builder)
      builder.instance_eval <<-EVAL
        def show?
          #{action_name == "show"}
        end
  EVAL
    end

在我的表单中(在 simple_form 块中):

<%- set_show_method_to_builder(f) -%>

最后,在 #app/inputs/string_input.rb 中:

class StringInput < SimpleForm::Inputs::StringInput
  def input
    if @builder.show?
      content_tag(:p, @builder.object[attribute_name], :class => :show)
    else
      super
    end
  end
end

未映射的数据类型存在一些问题,但这是另一个故事:
无法为某些(文本, 布尔值, ...) 类型,使用 SimpleForm

Here's my solution

in my application_helper.rb:

    def set_show_method_to_builder(builder)
      builder.instance_eval <<-EVAL
        def show?
          #{action_name == "show"}
        end
  EVAL
    end

In my forms (in the simple_form block):

<%- set_show_method_to_builder(f) -%>

And finally, in #app/inputs/string_input.rb:

class StringInput < SimpleForm::Inputs::StringInput
  def input
    if @builder.show?
      content_tag(:p, @builder.object[attribute_name], :class => :show)
    else
      super
    end
  end
end

There's some problem with data types not mapped, but it's another story:
Can't create Custom inputs for some (Text, Booleans, ...) types, with SimpleForm

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