【问题标题】:Can't puts parameter from yield不能从产量中放入参数
【发布时间】:2014-10-10 23:40:55
【问题描述】:

为什么我不能从产量中放入参数?

错误:

undefined local variable or method `options' for #<#<Class:0x007fd1bd8735e8>:0x007fd1ba3e4fe8>

/app/helpers/bootstrap_form_helper.rb

...
  def inline
    options = "row_disable"
    content_tag(:div, class: "row test") { yield(options) }
  end
...

/app/views/signup/new.html.erb

...    
<%= inline do %>
  <%= options %>
  <%= person_f.text_field(:last_name, control_col: 7) %>
<% end %>
...

【问题讨论】:

  • 你想做什么?
  • 因此,我尝试在 text_field 中传递此变量。但一开始,我不能把它放在浏览器中。

标签: ruby-on-rails yield


【解决方案1】:

您无法访问在inline 方法中定义的局部变量 options。您必须访问通过inline 方法传递给块的参数,为此您需要让new.html.erb 中的块接受options 参数:

  ...    
  <%= inline do |options| %>
    <%= options %>
    <%= person_f.text_field(:last_name, control_col: 7) %>
  <% end %>
  ...

进一步澄清一下,您甚至不需要在 new.html.erb 中将其称为 options。以下方法也可以:

  ...    
  <%= inline do |foo| %>
    <%= foo %>
    <%= person_f.text_field(:last_name, control_col: 7) %>
  <% end %>
  ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    相关资源
    最近更新 更多