为什么我的局部变量不会随着标签的变化而改变?

时间:2014-06-18 23:50:06

标签: ruby ruby-on-rails-3.2

我无法让我的本地变量在标签之间切换。本地设置在使用该表单的每个选项卡下的h3标记下。从那里,本地传递给类别变量,因此我的表单可以将其用作部分变量。目前,无论选择哪个标签,它都只会拉动第一个局部变量“Associates”。

有没有办法确保类别变量根据所选的标签获取传递给它的正确本地?

提前致谢。

_form.html.erb

<%= form_for(@best_practice) do |f| %>
<% if @best_practice.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(@best_practice.errors.count, "error") %> prohibited this best practice from being saved:</h2>

  <ul>
  <% @best_practice.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
  </div>
  <% end %>

  <div class="field" style="width:90%;">
  <%= f.label :title %><p class="tabbertab-lbreak"></p>
  <%= f.text_field :title %>
  <%= f.hidden_field :category, :value => @category %>
  </div>
  <div class="field" style="width:90%;">
  <%= f.label :body %><p class="tabbertab-lbreak"></p>
  <%= f.text_area :body, :cols => 30, :rows => 5 %>
  </div>
  <div class="actions">
  <%= f.submit(class: 'btn btn-large btn-primary') %>
  </div>
  <% end %>

 company.html.erb

 <div class="tabbable full-width-tabs">
        <ul class="nav nav-tabs nav-justified">
            <li class="active take-all-space-you-can"><a href="#tab-one" data-toggle="tab">Home</a></li>
            <li class="take-all-space-you-can"><a href="#tab-two" data-toggle="tab">Associate</a></li>
            <li class="take-all-space-you-can"><a href="#tab-three" data-toggle="tab">Safety</a></li>
            <li class="take-all-space-you-can"><a href="#tab-four" data-toggle="tab">Order Fulfillment</a></li>
        </ul>
        <div class="tab-content">
            <div class="tab-pane active" id="tab-one">
              <h3 class="splash-header">Welcome!</h3>
              <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur condimentum a purus vitae lobortis. Donec at nulla in lorem luctus semper. Nam porta dui eget pharetra pretium. Maecenas vitae porta est, sit amet auctor diam. Vestibulum mollis lectus mi, sed ultrices eros scelerisque in. Vivamus ultrices laoreet sem, id venenatis risus facilisis sit amet. Ut a hendrerit nisi, ut laoreet dui.</p><p>Proin laoreet massa sed dictum elementum. Nulla quis suscipit nisi. Etiam aliquam at nibh et feugiat. Pellentesque gravida lorem id erat elementum accumsan. Fusce sit amet enim nec nunc hendrerit molestie. Ut quis orci viverra tortor lobortis fringilla. Aenean in ligula dui. Morbi ornare malesuada augue, sed elementum purus varius ut. Fusce suscipit orci id diam tristique, ut semper lorem cursus. Proin urna massa, pulvinar at sapien vitae, tempor cursus sem. Nunc sit amet nibh sit amet odio luctus ultrices vel congue sem. Maecenas elementum, turpis nec cursus volutpat, diam quam faucibus ipsum, pellentesque tincidunt mi velit eget ligula. Aenean elementum, neque vel accumsan rutrum, risus diam posuere risus, sit amet semper risus magna sed nibh. Pellentesque quis risus at dui bibendum ornare ut vel augue. Aliquam et nunc sit amet dolor vehicula eleifend.</p>                    
            </div>
            <div class="tab-pane" id="tab-two">
                <h3 class="splash-header">Post Best Practice</h3>
                   <% @associate = "Associate" %>
                   <%= render 'best_practices/form', local: {category: @associate} %>
                  <h3 class="splash-header">View Best Practice</h3>
                   <ul>
                   <% BestPractice.with_category("Associate").each do |bp| %>
                   <li><%= link_to bp.title, best_practice_path(bp) %></li>
                   <% end %>
                  </ul>     
            </div>
            <div class="tab-pane" id="tab-three">
                <h3 class="splash-header">Post Best Practice</h3>
                  <% @safety = "Safety" %>
                  <%= render 'best_practices/form', local: {category: @safety} %>
                  <h3 class="splash-header">View Best Practice</h3>
                   <ul>
                   <% BestPractice.with_category("Safety").each do |bp| %>
                   <li><%= link_to bp.title, best_practice_path(bp) %></li>
                   <% end %>
                  </ul>
            </div>
            <div class="tab-pane" id="tab-four">
                <h3 class="splash-header">Post Best Practice</h3>
                  <% @order = "Order Fulfillment" %>
                  <%= render 'best_practices/form', local: {category: @order}  %>
                    <h3 class="splash-header">View Best Practice</h3>
                     <ul>
                      <% BestPractice.with_category("Order Fulfillment").each do |bp| %>
                      <li><%= link_to bp.title, best_practice_path(bp) %></li>
                     <% end %>
                     </ul>
            </div>  
        </div> 
    </div>

2 个答案:

答案 0 :(得分:0)

_form部分内容中,您需要将category引用为category,而不是@category

此外,local应为locals,我认为您必须使用render密钥呼叫partial以传入本地,例如:

render partial: 'best_practices/form', locals: {category: @order} 

答案 1 :(得分:0)

_form.html.erb需要简单地使用category而不是@category

对于您的company.html.erb,请尝试

 <%= render 'best_practices/form', :category => @associate %>

这会将@associate分配给:category变量,另一方面可以将其作为简单category进行访问。