加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

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

发布时间:2020-12-14 18:40:28 所属栏目:资源 来源:网络整理
导读:当我使用rails时,我使用简单的表单gem创建一个简单的表单 我创建了一个这样的表单 %= simple_form_for @user do |f| % %= f.input :username % %= f.input :password % %= f.button :submit %% end % 但我想在每个字段旁边添加一个默认的图像元素.我怎么能实
当我使用rails时,我使用简单的表单gem创建一个简单的表单

我创建了一个这样的表单

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

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

我试试这个

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

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

解决方法

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

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

然后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

然后在HTML方面

做这个

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

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

希望这有帮助

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读