【问题标题】:Custom helper - dependencies link_to remote自定义助手 - 依赖链接到远程
【发布时间】:2013-02-13 13:02:13
【问题描述】:

我目前正在编写一个基于引导程序的自定义表单助手,但我遇到了依赖问题(我认为):

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 #<..>

【问题讨论】:

  • 请指定错误的行号和文件名
  • 错误出现在我的帮助文件中的 link_to 行。我认为在网址上:link_to raw("..."), {:controller => "myController", :action => "myAction"}, :method => "post", :remote => true, :html => { ... } 缺少包含?

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


【解决方案1】:

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

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

试一试:

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

【讨论】:

  • 感谢回复。但是在 the API doc 中,如果使用额外的文字哈希,则不是强制性的。但我已经测试过,没有任何改变。
  • 我认为您发布时使用 :as 选项,因此您需要 :url 或者帮助者需要它来工作而不是控制器。请参阅链接的最后一行。
  • 我已经测试过使用像link_to raw("..."), my_route_name_path, ...这样的路由名称。它返回未定义的变量或方法“my_route_name_path”。与别名 (:as) 相同。缺少包含?
  • 这是因为没有名称为my_route_name_path 的路由。您应该尝试运行rake routes 以确定您拥有哪些有效路线,然后放入link_to。
  • 路由名称正确。实际上,这是 jQuery 与魅力一起工作的。但我将用 link_to remote 替换我的事件处理程序附件 (.on())...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-09
  • 2011-01-29
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
  • 2015-03-10
相关资源
最近更新 更多