下拉保存#而不是ID

时间:2014-08-21 11:13:33

标签: ruby-on-rails rails-activerecord erb

我设法获得了2个关联表的下拉列表,其中显示了项目的名称,并且应该将ID保存在其中(如您在下面的视图提取中所示):

  <div class="field">
    <%= f.label :period_id %><br>
    <%= f.collection_select(:period_id, Period.all, :id, :name) %>
  </div>
  <div class="field">
    <%= f.label :type_id %><br>
    <%= f.collection_select(:type_id, Type.all, :id, :name) %>
  </div>

但是,即使我可以选择句点和类型,也始终保存以下内容:

  

期间:#&lt;期间:0x554c388&gt;

     

类型:#&lt;键入:0x55511e8&gt;

(我将<&lt;&gt;间隔开,因为没有空格,它不会显示)

有什么想法吗?

输出HTML如下:

    <!DOCTYPE html>
<html>
<head>
  <title>Coke</title>
  <link data-turbolinks-track="true" href="/assets/logs.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/periods.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/scaffolds.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/types.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/application.css?body=1" media="all" rel="stylesheet" />
  <script data-turbolinks-track="true" src="/assets/jquery.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/jquery_ujs.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/turbolinks.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/logs.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/periods.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/types.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/application.js?body=1"></script>
  <meta content="authenticity_token" name="csrf-param" />
<meta content="m82OedrxwaB4FwxkG8mIdHH1SeFL2lY7r6LGUbdhPyI=" name="csrf-token" />
</head>
<body>

<h1>New log</h1>

<form accept-charset="UTF-8" action="/logs" class="new_log" id="new_log" method="post"><div style="display:none"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="m82OedrxwaB4FwxkG8mIdHH1SeFL2lY7r6LGUbdhPyI=" /></div>

  <div class="field">
    <label for="log_period_id">Period</label><br>
    <select id="log_period_id" name="log[period_id]"><option value="1">August 2014</option></select>
  </div>
  <div class="field">
    <label for="log_type_id">Type</label><br>
    <select id="log_type_id" name="log[type_id]"><option value="1">Petrol</option>
<option value="2">Food</option></select>
  </div>
  <div class="field">
    <label for="log_logdate">Logdate</label><br>
    <select id="log_logdate_1i" name="log[logdate(1i)]">
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option selected="selected" value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
</select>
<select id="log_logdate_2i" name="log[logdate(2i)]">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option selected="selected" value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select id="log_logdate_3i" name="log[logdate(3i)]">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option selected="selected" value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>

  </div>
  <div class="field">
    <label for="log_amount">Amount</label><br>
    <input id="log_amount" name="log[amount]" type="text" />
  </div>
  <div class="field">
    <label for="log_notes">Notes</label><br>
    <textarea id="log_notes" name="log[notes]">
</textarea>
  </div>
  <div class="actions">
    <input name="commit" type="submit" value="Create Log" />
  </div>
</form>

<a href="/logs">Back</a>


</body>
</html>

1 个答案:

答案 0 :(得分:1)

所以,问题不在于你的形式。问题是如何输出关联。

我猜你这样做

<%= log.period %>

相反,你需要

<%= log.period.name %>