自定义帮助程序 - 依赖项link_to remote

时间:2013-02-13 13:02:14

标签: ruby-on-rails-3 twitter-bootstrap dependencies helper

我目前正在编写一个基于bootstrap的自定义表单助手,我遇到依赖项问题(我认为):

class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
  include ActionView::Helpers::UrlHelper
  include ActionView::Helpers::OutputSafetyHelper

  alias :super_collection_select :collection_select

  def collection_select_append(method, collection, value_method, text_method, options = {}, html_options = {})
    ('<div class="control-group' + has_error(object.errors[method]) + '">' +
      label(method, :class => "control-label") +
      '<div class="controls">' +
        '<div class="input-append">' +
          super_collection_select(method, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options)) +
          '<span class="add-on">' +
            link_to(raw("<i class\"icon-white icon-ok\"></i>"), {:controller => "myController", :action => "myAction"}, :method => "post", :remote => true, :html => { :val1 => "val1", :val2 => "val2" } ) +
          '</span>' +
        '</div>' +
        get_error_message(object.errors[method]) +
      '</div>' +
    '</div>').html_safe
  end

end

这是一个错误:

undefined local variable or method `controller' for #<..>

1 个答案:

答案 0 :(得分:0)

我认为您错过了控制器的:url选项:

link_to(raw("<i class\"icon-white icon-ok\"></i>"), {:controller => "myController", :action => "myAction"}, :method => "post", :remote => true, :html => { :val1 => "val1", :val2 => "val2" } )

TRY:

class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
  include ActionView::Helpers::UrlHelper
  include ActionView::Helpers::OutputSafetyHelper

  alias :super_collection_select :collection_select

  def collection_select_append(method, collection, value_method, text_method, options = {}, html_options = {})
    ('<div class="control-group' + has_error(object.errors[method]) + '">' +
      label(method, :class => "control-label") +
      '<div class="controls">' +
        '<div class="input-append">' +
          super_collection_select(method, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options)) +
          '<span class="add-on">' +
            link_to(raw("<i class\"icon-white icon-ok\"></i>"), :url => {:controller => "myController", :action => "myAction"}, :method => "post", :remote => true, :html => { :val1 => "val1", :val2 => "val2" } ) +
          '</span>' +
        '</div>' +
        get_error_message(object.errors[method]) +
      '</div>' +
    '</div>').html_safe
  end

end