Ruby on Rails:简单的表单,在每个输入字段旁边添加Image

时间:2022-02-19 16:46:17

When i use rails, i create a simple form using simple form gem

当我使用rails时,我使用简单的表单gem创建一个简单的表单

I create a form like this

我创建了一个这样的表单

<%= simple_form_for @user do |f| %>
  <%= f.input :username %>
  <%= f.input :password %>
  <%= f.button :submit %>
<% end %>

but i would like to add an default image element just next to each fields. how can i achieve that?

但我想在每个字段旁边添加一个默认的图像元素。我怎么能实现这一目标?

i try this

我试试这个

<%= simple_form_for @user do |f| %>
      <%= f.input :username % >
      <img xxxx>
      <%= f.input :password %>
      <img xxxx>
      <%= f.button :submit %>
      <img xxxx>
    <% end %>

but i won't work as it would wrap into a new line for each element field.

但我不会工作,因为它将包装为每个元素字段的新行。

3 个解决方案

#1


1  

Well the answer for that is is like this create a folder called input in the app/

那么答案就是这样在app中创建一个名为input的文件夹/

and create a file with [any_name]_input.rb let say you name it image_tag_input.rb

并使用[any_name] _input.rb创建一个文件,假设您将其命名为image_tag_input.rb

then the code inside the image_tag_input.rb look like this

然后image_tag_input.rb中的代码看起来像这样

 class ImageTagInput <  SimpleForm::Inputs::Base
   ## Because image tage doesnot work if not included the below
   include ActionView::Helpers::AssetTagHelper
   def input
    ("#{@builder.text_field(attribute_name, input_html_options)}" + "#{image_tag()}).html_safe
   end
end

and then on the html side

然后在HTML方面

do this

<%= f.input :username,:as => "image_tag %>

Here and another example mention in wiki of the simple_form_for

这里和另一个例子在wiki中提到simple_form_for

Hope this help

希望这有帮助

#2


0  

I always use wrappers that Simple Form Bootstrap uses.

我总是使用Simple Form Bootstrap使用的包装器。

An example from its default wrapper:

默认包装器的一个示例:

SimpleForm.setup do |config|
  config.error_notification_class = 'alert alert-danger'
  config.button_class = 'btn btn-default'
  config.boolean_label_class = nil

  # :vertica_from wrapper
  config.wrappers :vertical_form, tag: 'div', class: 'form-group row', error_class: 'has-error' do |b|
    b.use :html5
    b.use :placeholder
    b.optional :maxlength
    b.optional :pattern
    b.optional :min_max
    b.optional :readonly
    b.use :label, class: 'control-label'

    b.use :input, class: 'form-control'
    b.use :error, wrap_with: { tag: 'span', class: 'help-block' }
    b.use :hint,  wrap_with: { tag: 'p', class: 'help-block' }
  end


  # Rest omitted

  # Change default simple form wrapper settings
  config.default_wrapper = :vertical_form
  config.wrapper_mappings = {
    check_boxes: :vertical_radio_and_checkboxes,
    radio_buttons: :vertical_radio_and_checkboxes,
    file: :vertical_file_input,
    boolean: :vertical_boolean,
    datetime: :multi_select,
    date: :multi_select,
    time: :multi_select
  }
end

When you create a simple form

当您创建一个简单的表单

<%= form_for(resource, as: resource_name, url: unlock_path(resource_name), html: { method: :post }) do |f| %>

It will by default(because of the setting) to use :vertical_form

默认情况下(因为设置)将使用:vertical_form

Now if you want to set up a custom wrapper, follow the example above, create your custom class and place it under config/initializers. This is an example custom wrapper I added the that Bootstrap setup above:

现在,如果要设置自定义包装器,请按照上面的示例创建自定义类并将其放在config / initializers下。这是一个示例自定义包装器我添加了上面的Bootstrap设置:

config.wrappers :horizontal_file_input, tag: 'div', class: 'form-group row', error_class: 'has-error' do |b|
    b.use :html5
    b.use :placeholder
    b.optional :maxlength
    b.optional :readonly
    b.wrapper tag: 'div', class: 'col-md-6' do |bb|
      bb.use :label, class: 'col-sm-5 control-label'

      bb.wrapper tag: 'div', class: 'col-sm-7' do |bbb|
        bbb.use :input
        bbb.use :hint,  wrap_with: { tag: 'p', class: 'help-block' }
      end
    end

    b.wrapper tag: 'div', class: 'col-md-3 side-validation' do |bc|
      bc.use :error, wrap_with: { tag: 'span', class: 'help-block' }
    end
  end

Then to use this wrapper, find the input where you want to apply custom wrapper, and do this:

然后使用此包装器,找到要应用自定义包装器的输入,并执行以下操作:

<%= f.input :resume, as: :attachment_preview, wrapper: :horizontal_file_input %>

Boom! It will be rendered with your custom settings! Also, you can set wrapper to the form to alter the default wrappers for all of its inputs. So if you do:

繁荣!它将使用您的自定义设置呈现!此外,您可以将包装器设置为表单以更改其所有输入的默认包装器。所以如果你这样做:

<%= simple_form_for(@staff,  as: :staff,
                             url: staffs_path, 
                             method: "post",
                             wrapper:  :horizontal_form) do |f| %>

Then all of its input fields will default to use :horizontal_form wrapper (which is also another Simple Form Bootstrap wrapper)

然后它的所有输入字段都将默认使用:horizo​​ntal_form wrapper(这也是另一个Simple Form Bootstrap包装器)

Hope this helps.

希望这可以帮助。

#3


-1  

I would suggest looking at using f.label and f.input_field instead of f.input since it would allow you to place the components as you would like and adding your images where you want them.

我建议使用f.label和f.input_field而不是f.input,因为它允许您根据需要放置组件并将图像添加到您想要的位置。

Check out the Simple_Form documentation for more information and examples.

查看Simple_Form文档以获取更多信息和示例。

Example:

<%= simple_form_for @user do |f| %>
  <%= f.label :username %>
  <%= f.input_field :username %>
  <img xxxx>
  <%= f.button :submit %>
<% end %>

#1


1  

Well the answer for that is is like this create a folder called input in the app/

那么答案就是这样在app中创建一个名为input的文件夹/

and create a file with [any_name]_input.rb let say you name it image_tag_input.rb

并使用[any_name] _input.rb创建一个文件,假设您将其命名为image_tag_input.rb

then the code inside the image_tag_input.rb look like this

然后image_tag_input.rb中的代码看起来像这样

 class ImageTagInput <  SimpleForm::Inputs::Base
   ## Because image tage doesnot work if not included the below
   include ActionView::Helpers::AssetTagHelper
   def input
    ("#{@builder.text_field(attribute_name, input_html_options)}" + "#{image_tag()}).html_safe
   end
end

and then on the html side

然后在HTML方面

do this

<%= f.input :username,:as => "image_tag %>

Here and another example mention in wiki of the simple_form_for

这里和另一个例子在wiki中提到simple_form_for

Hope this help

希望这有帮助

#2


0  

I always use wrappers that Simple Form Bootstrap uses.

我总是使用Simple Form Bootstrap使用的包装器。

An example from its default wrapper:

默认包装器的一个示例:

SimpleForm.setup do |config|
  config.error_notification_class = 'alert alert-danger'
  config.button_class = 'btn btn-default'
  config.boolean_label_class = nil

  # :vertica_from wrapper
  config.wrappers :vertical_form, tag: 'div', class: 'form-group row', error_class: 'has-error' do |b|
    b.use :html5
    b.use :placeholder
    b.optional :maxlength
    b.optional :pattern
    b.optional :min_max
    b.optional :readonly
    b.use :label, class: 'control-label'

    b.use :input, class: 'form-control'
    b.use :error, wrap_with: { tag: 'span', class: 'help-block' }
    b.use :hint,  wrap_with: { tag: 'p', class: 'help-block' }
  end


  # Rest omitted

  # Change default simple form wrapper settings
  config.default_wrapper = :vertical_form
  config.wrapper_mappings = {
    check_boxes: :vertical_radio_and_checkboxes,
    radio_buttons: :vertical_radio_and_checkboxes,
    file: :vertical_file_input,
    boolean: :vertical_boolean,
    datetime: :multi_select,
    date: :multi_select,
    time: :multi_select
  }
end

When you create a simple form

当您创建一个简单的表单

<%= form_for(resource, as: resource_name, url: unlock_path(resource_name), html: { method: :post }) do |f| %>

It will by default(because of the setting) to use :vertical_form

默认情况下(因为设置)将使用:vertical_form

Now if you want to set up a custom wrapper, follow the example above, create your custom class and place it under config/initializers. This is an example custom wrapper I added the that Bootstrap setup above:

现在,如果要设置自定义包装器,请按照上面的示例创建自定义类并将其放在config / initializers下。这是一个示例自定义包装器我添加了上面的Bootstrap设置:

config.wrappers :horizontal_file_input, tag: 'div', class: 'form-group row', error_class: 'has-error' do |b|
    b.use :html5
    b.use :placeholder
    b.optional :maxlength
    b.optional :readonly
    b.wrapper tag: 'div', class: 'col-md-6' do |bb|
      bb.use :label, class: 'col-sm-5 control-label'

      bb.wrapper tag: 'div', class: 'col-sm-7' do |bbb|
        bbb.use :input
        bbb.use :hint,  wrap_with: { tag: 'p', class: 'help-block' }
      end
    end

    b.wrapper tag: 'div', class: 'col-md-3 side-validation' do |bc|
      bc.use :error, wrap_with: { tag: 'span', class: 'help-block' }
    end
  end

Then to use this wrapper, find the input where you want to apply custom wrapper, and do this:

然后使用此包装器,找到要应用自定义包装器的输入,并执行以下操作:

<%= f.input :resume, as: :attachment_preview, wrapper: :horizontal_file_input %>

Boom! It will be rendered with your custom settings! Also, you can set wrapper to the form to alter the default wrappers for all of its inputs. So if you do:

繁荣!它将使用您的自定义设置呈现!此外,您可以将包装器设置为表单以更改其所有输入的默认包装器。所以如果你这样做:

<%= simple_form_for(@staff,  as: :staff,
                             url: staffs_path, 
                             method: "post",
                             wrapper:  :horizontal_form) do |f| %>

Then all of its input fields will default to use :horizontal_form wrapper (which is also another Simple Form Bootstrap wrapper)

然后它的所有输入字段都将默认使用:horizo​​ntal_form wrapper(这也是另一个Simple Form Bootstrap包装器)

Hope this helps.

希望这可以帮助。

#3


-1  

I would suggest looking at using f.label and f.input_field instead of f.input since it would allow you to place the components as you would like and adding your images where you want them.

我建议使用f.label和f.input_field而不是f.input,因为它允许您根据需要放置组件并将图像添加到您想要的位置。

Check out the Simple_Form documentation for more information and examples.

查看Simple_Form文档以获取更多信息和示例。

Example:

<%= simple_form_for @user do |f| %>
  <%= f.label :username %>
  <%= f.input_field :username %>
  <img xxxx>
  <%= f.button :submit %>
<% end %>