【问题标题】:Passing in options param for content_for method为 content_for 方法传入选项参数
【发布时间】:2012-10-26 17:12:48
【问题描述】:

我发现你可以通过查看 Rails 源代码来刷新content_forhttps://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/capture_helper.rb

Rails 来源:

def content_for(name, content = nil, options = {}, &block)
  if content || block_given?
    if block_given?
      options = content if content
      content = capture(&block)
    end
    if content
      options[:flush] ? @view_flow.set(name, content) : @view_flow.append(name, content)
    end
    nil
  else
    @view_flow.get(name)
  end
end

我正在尝试设置options[:flush] = true,但遇到了一些问题。 options[:flush] 在我下面的代码中没有评估为真。

我的代码

content_for(affiliate_link_pos.to_sym, {:flush=>true}) do
  render page
end

编辑: 我也尝试过传递第三个参数(内容),但我得到了wrong number of argument error (3 for 2)

content_for(affiliate_link_pos.to_sym, "true",  {:flush=>true}) do

【问题讨论】:

  • 刚刚弄清楚原因。我在 Rails 3.2.6 上,content_for 源是 3.2.8。

标签: ruby-on-rails ruby-on-rails-3


【解决方案1】:

试试这个:

content_for(affiliate_link_pos.to_sym, null, :flush=>true) do
  render page
end

【讨论】:

    【解决方案2】:

    看起来 OP 问题中引用的来源实际上来自 Rails 4。Even in Rails 3.2.14, content_for does not accept any options.

    content_for(名称,内容 = nil,&block)

    调用#content_for 将标记块存储在标识符中以供以后使用。您可以通过将标识符作为参数传递给 content_for 来对其他模板、帮助模块或布局中存储的内容进行后续调用。

    【讨论】:

    • 不鼓励仅链接的答案。
    猜你喜欢
    • 2018-01-23
    • 2021-08-17
    • 1970-01-01
    • 2016-01-22
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多