Custom HTML attribute requires a custom helper?

拈花ヽ惹草 提交于 2019-12-09 14:12:40

问题


I'm trying to create a form with some custom data attributes on the inputs:

<input type="text" data-family="Dinosaurs">

This seemed like a nice clean way to have easy front-end access (haha!) with jquery:

$("[data-family='Dinosaurs']").doSomething()

The problem is I can't get Rails (3.0.3) to render the attribute.

<%= f.text_field :question, :id=>"poll_question", :class=>"BigInput", :style=>"width:98%;", :attributes=>"data-submit_clear='1'" %>

I've tried many permutations to no avail and can't find an example of how to do this. Do I need to modify the text_field helper to support any custom attributes?


回答1:


Oops. It's just

<%= f.text_field :question, :id=>"poll_question", :class=>"BigInput", :style=>"width:98%;", 'data-submit_clear'=>'1' %>



回答2:


Rails >3.1 has a handy shortcut for data-attributes like this which most HTML-generating helpers support:

<%= f.text_field :question, :data => { :submit_clear => '1' } %>

It can make things more readable when you have a couple of data attributes, e.g.:

<%= f.text_field :question, :data => { :submit_clear => '1', :more_info => 'Ok', :also => 'this' } %>


来源:https://stackoverflow.com/questions/5200260/custom-html-attribute-requires-a-custom-helper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!