自定义表单助手 [英] Custom form helpers

查看:231
本文介绍了自定义表单助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以创建自定义表单助手,而不是:

  special_field_tag:object,:method 

我可以达到这样的效果:

 form.special_field:method 


解决方案

是的,您可以添加到FormBuilder类中,并访问传递给form_for的对象。我已经做了很多事情:日期,时间,度量等等。下面是一个例子:

  class ActionView: :Helpers :: FormBuilder 
包括ActionView :: Helpers :: TagHelper
包括ActionView :: Helpers :: FormTagHelper
包括ActionView :: Helpers :: FormOptionsHelper
包括ActionView :: Helpers :: CaptureHelper
包括ActionView :: Helpers :: AssetTagHelper

#接受一个int并根据<,<或= 0
显示笑脸def smile_tag(方法,options = {})
value = @ object.nil? ? 0:@ object.send(method).to_i
options [:id] = field_id(method,options [:index])
smiley =: - |
if value> 0
smiley =:-)
elsif笑脸< 0
smiley =:-(
end
return text_field_tag(field_name(method,options [:index]),options)+ smiley
end

def field_name(label,index = nil)
output = index?[#{index}]:''
return @object_name + output +[#{label}]
end
$ b def field_id(label,index = nil)
output = index?_#{index}:''
return @object_name + output +_ #{label}
end

end

可以这样使用:

 <%form_for @quiz do | f |%> 
<%= f.smile_tag(:score)%>
<%end%>

有一些由Rails创建的实例变量,您可以在这些帮助器方法中访问: b
$ b


  • @object - 由表单

  • @object_name - 对象的类名称

  • @template - 我认为它是ActionView的一个实例,通过在模板上调用方法来添加我添加的所有包含。还没有尝试过。

  • @options - 通过form_for调用创建的选项传递给FormBuilder的选项



我写了field_id和field_name方法,以便像常规帮助程序那样在HTML输入元素上创建这些属性,我确信有一种方法可以与Rails使用的相同方法绑定,但我还没有找到它。



天空是你可以用这些辅助方法做的限制,它们只是返回字符串。您可以一次创建完整的HTML表格或页面,但您最好有一个很好的理由。

这个文件应该添加到app / helpers文件夹中。


Is there a way that I can create a custom form helper so that instead of:

special_field_tag :object, :method

I can achieve something like:

form.special_field :method

解决方案

Yes, you can add to the FormBuilder class and get access to the object passed into the form_for. I've done this for a lot of things: dates, times, measurements, etc. Heres an example:

class ActionView::Helpers::FormBuilder
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::FormTagHelper
  include ActionView::Helpers::FormOptionsHelper
  include ActionView::Helpers::CaptureHelper
  include ActionView::Helpers::AssetTagHelper

  # Accepts an int and displays a smiley based on >, <, or = 0
  def smile_tag(method, options = {})
    value = @object.nil? ? 0 : @object.send(method).to_i
    options[:id] = field_id(method,options[:index])
    smiley = ":-|"
    if value > 0
      smiley = ":-)"
    elsif smiley < 0
       smiley = ":-("
    end
    return text_field_tag(field_name(method,options[:index]),options) + smiley
  end

  def field_name(label,index=nil)
    output = index ? "[#{index}]" : ''
    return @object_name + output + "[#{label}]"
  end

  def field_id(label,index=nil)
    output = index ? "_#{index}" : ''
    return @object_name + output + "_#{label}"
  end

end

Which you can use like this:

<% form_for @quiz do |f| %>
  <%= f.smile_tag(:score) %>
<% end %>

There are some instance variables created by Rails that you can access in these helper methods:

  • @object - the model object specified by the form
  • @object_name - the class name of the object
  • @template - I think its an instance of the ActionView, you can possibly bypass all the includes I added by calling methods on the template. Haven't tried that yet.
  • @options - options passed to the FormBuilder when its created by the form_for call

I wrote the field_id and field_name methods to create these attributes on the HTML input elements the same way the regular helpers do, I'm sure there is a way to tie into the same methods that Rails uses, but I haven't found it yet.

The sky is the limit on what you can do with these helper methods, they simply return strings. You can create entire HTML tables or pages in one, but you better have a good reason to.

This file should be added in the app/helpers folder

这篇关于自定义表单助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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