在渲染中具有变量将转移到HTML

时间:2013-01-11 21:11:36

标签: ruby-on-rails erb

很抱歉这个令人困惑的标题,但我不确定究竟是什么问题,因为我不太了解Ruby。无论如何,关于这个问题!

所以我有一个渲染:

<div class="form" id="dept_div">
     <span class="label search_label">Department:</span>
          <% query = "<option></option>" %>
             <% Department.all.each do |d| %>
                <% query << "<option>"+d.dept+"</option>" %>
             <% end %>

          <%=select_tag :major, query.html_safe,   class: "search_tag", id: "dept_drop" %>
 </div>

我尝试插入这里:

<span class="su_label">
  <%= f.label :major %>
</span>
<% if !in_mobile_view? %>
  <div class = "su_textfield">
    <div class="checkbox">
    <% else %>
      <div data-role="fieldcontain" class="field_group">
        <fieldset data-role="controlgroup" data-type="horizontal">
        <% end %>
        <!-- check privacy preferences to decide whether box should already be checked -->
        <% if !hasnoprefs && @user.privacy_prefs.include?("nomajor") %>
          <%= check_box_tag "major", 1, true %>
        <% else %>
          <%= check_box_tag "major" %>
        <% end %>
        <%= label_tag 'major', 'Hide' %>
        <% if !in_mobile_view? %>
        </div>
        <%= render 'courses/form_partials/majors' %>
</div>
    <% else %>
      <%= f.text_field :major, class: "settings_inputfield" %>
    </fieldset>

我想要的是变量major(在类User中)成为从渲染中选择的项目(这是一个下拉菜单)。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试......

<%= f.collection_select :major, query, :id, :major, {:include_blank => 'None' } %>