Ruby on Rails : simple form, add Image next to eac

2019-06-18 00:58发布

问题:

When i use rails, i create a simple form using simple form 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.

回答1:

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

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

then the code inside the image_tag_input.rb look like this

 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

do this

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

Here and another example mention in wiki of the simple_form_for

Hope this help



回答2:

I always use wrappers that Simple Form Bootstrap uses.

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

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.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)

Hope this helps.



回答3:

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.

Check out the Simple_Form documentation for more information and examples.

Example:

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