Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails : simple form, add Image next to each input field

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.

like image 802
TheOneTeam Avatar asked Aug 22 '12 11:08

TheOneTeam


1 Answers

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

like image 133
Viren Avatar answered Oct 21 '22 09:10

Viren