我有一个带有“simpleform”和“bootstrap-datepicker-rails”宝石的rails应用程序。
我尝试使视图代码更具可读性:
f.input :birthday, :mydatefield, :options => {:icon => "calendar", :data-date => "21.12.2012", :data-format => "dd-mm-yyyy"}
此刻我写下这个:
<div class="input-append date" id="user_birthday" data-date="12-02-2012" data-date-format="dd-mm-yyyy">
<%= f.input_field :birthday, :disabled => true %>
<span class="add-on">
<i class="icon-calendar"></i>
</span>
</div>
或者
<%= f.input :birthday, :wrapper => :datepicker, :wrapper_html=> {id: "user-birthday", class: "date"} do %>
<%= f.input_field :birthday, :disabled => true %>
<%= content_tag :span, :class => "add-on" do %>
<%= content_tag :i, :style => "color:grey", :class => "icon-calendar" do
end %>
<% end %>
<% end %>
得到我的结果:
以上两个都是这个结果,效果很好:
<div class="control-group string required date" id="user-birthday">
<label class="string required control-label" for="user_birthday"><abbr title="required">*</abbr> Birthday</label>
<div class="controls">
<div class="input-append">
<input class="string required disabled" disabled="disabled" id="user_birthday" name="user[birthday]" size="50" type="text" />
<span class="add-on">
<i class="icon-calendar" style="color:grey"></i>
</span>
</div>
</div>
</div>
当我尝试将变量解析为包装器
时:wrapper_html=> => {:data-date => "21-12-2012"}
它会抛出错误......
我该怎么办?我很擅长扩展宝石。
我必须写些什么才能让它发挥作用?
一个例子非常有帮助!
非常感谢提前!
答案 0 :(得分:0)
您可以创建自定义字段。按照this tutorial进行操作。
答案 1 :(得分:0)
我已经切换回rails实现的表单。对我来说(作为一个初学者),simple_form似乎不够清晰。
对于我上面的解决方案,我创建了一个FormBuilder:
class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
def birthday_field_with_hint_and_icons (name, *args)
options = args.extract_options!
birthday_field_icons(name) + hint(options[:hint])
end
def birthday_field_icons(name, *args)
dd = 'data-date'
ddf = 'data-date-format'
content_tag :div, class: "input-append date", dd => 18.years.ago.strftime("%d-%m-%Y"), ddf => "dd-mm-yyyy", id: "user_birthday" do
text_field(name, :disabled => 'disabled') + icon("icon-calendar")
end
end
private
def icon(icon)
content_tag :span, class: "add-on" do
content_tag :i, style: "color: grey;", class: icon do
end
end
end
def hint(hint)
content_tag :span, class: "help-block" do
hint
end
end
end
感谢您的帮助!