简体   繁体   中英

Can't puts parameter from yield

Why I can't puts parameter from yield?

Error:

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

You cannot access the local variable , options , defined in the inline method. You have to access the argument passed to the block by the inline method, and to do that you need to have your block in new.html.erb accept an options argument:

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

Just to clarify a little further, you don't even need to call it options in new.html.erb. The following would also work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM