Ruby on Rails:简单的形式,在每个输入字段旁边添加Image [英] Ruby on Rails : simple form, add Image next to each input field

查看:116
本文介绍了Ruby on Rails:简单的形式,在每个输入字段旁边添加Image的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我试试这个

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


推荐答案

好的,就是像这样在应用程序中创建一个名为input的文件夹/

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

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

,那么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

然后在html方面

做到这一点

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

这里和另一个例子在 simple_form_for

希望得到这个帮助

这篇关于Ruby on Rails:简单的形式,在每个输入字段旁边添加Image的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆