简体   繁体   中英

Rails 3.2 + Ruby 1.9 + block variables

What's standard practice for Rails now? Before Ruby 1.9 I could do this...

<% @records.each do |@record| %>
  <%= form_tag "/do/something" do %>
    <%= text_field 'record', 'text' %>
  <% end %>
  <%= a_display_record_helper %>
  <%= render :partial => "record_partial" %>
<% end %>

But now we can't use instance variables in blocks. So what are standard practices for blocks containing forms, view helpers and partials?

In ruby 1.9 the block variable name cannot start with @ :

<% @records.each do |record| %>
  <%= form_tag "/do/something" do %>
    <%= text_field 'record', 'text' %>
  <% end %>
  <%= a_display_record_helper %>
  <%= render :partial => "record_partial" %>
<% end %>

You can still pass the record variable to the helper as a parameter and for the partial too. It is easier to understand what is going on if you do that, anyway.

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