【问题标题】:passing local variable to partial将局部变量传递给部分
【发布时间】:2013-12-12 11:08:04
【问题描述】:

这个问题已经被问过很多次了,但是在尝试了我能找到的所有解决方案之后,我并没有更接近自己的解决方案。

我有一个部分,_form - 这是相关部分:

<% @tournament.sub_tournaments.each do |sub| %>
  <%= f.fields_for :sub_tournaments, sub, :validate => false do |sub_form| %>
    ...
      <table>
        ...
        <tbody>
        <%= sub_form.fields_for :standings, :validate => false do |standings| %>
          <%= render 'tournaments/form_partials/standing_fields', f: standings, sub: sub %> #here's the problem
        <% end %>  
        </tbody>
      </table>
      ...
  <% end %>
<% end %>

在标记的行中,我试图将两个变量传递给给定的部分 - f: standingssub: sub。如果我只通过f: standings,页面加载正常,但是当我添加sub: sub时,我收到以下错误:

undefined local variable or method `sub'

它引用了_standing_fields部分中的以下标记行:

<tr>
  <td><%= f.hidden_field :_destroy %><%= f.text_field :standing, :class => "standing", readonly: true, :type => "" %></td>
  <% if Game::TEAM_GAMES.include? sub.game.name %> #here's the problem
    <td><%= f.grouped_collection_select(:team_division_id, Team.order(:name).includes(:team_divisions), :team_divisions, :name, :id, :game_name, include_blank: true)%></td>
  <% else %>
    <td><%= f.grouped_collection_select(:player_id, Player.order(:name).includes(:player), :player, :name, :id, :game_name, include_blank: true)%></td>
  <% end %>
  <td><span class="remove">Remove</span></td>
</tr>

我想不出出了什么问题,我已经三重检查了语法、拼写、逗号等,并按照建议的一种解决方案重新启动了我的 redis 服务器和 rails 服务器。

【问题讨论】:

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


    【解决方案1】:

    您必须将变量放在本地键中:

    <%= render 'tournaments/form_partials/standing_fields', locals: {f: standings, sub: sub} %>
    

    【讨论】:

    • 不是这样 - 一定有其他东西触发了这种情况,因为如果我在_form 中注释掉有问题的行,我仍然会收到错误消息。无论如何-为什么locals键?在其他情况下,我已经将多个变量提交给没有它的局部变量,并且它工作正常..?
    • 发现了错误,这是一个完全不同的古怪事情——尽管你的回答对我提出的问题是正确的。如果您知道 locals 密钥的必要性,请告诉我!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多