不能从yield中输入参数

时间:2014-08-18 05:31:30

标签: ruby-on-rails yield

为什么我不能从yield中输入参数?

错误:

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

1 个答案:

答案 0 :(得分:0)

您无法访问options方法中定义的本地变量inline。您必须通过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 %>
  ...
相关问题