Rails 請教如何在 view 產生一個陣列的具體作法?

idarfan · 2012年05月02日 · 最后由 Ddl1st 回复于 2012年05月03日 · 2341 次阅读

這個是我 view 的頁面 View的頁面 具體的程式碼如下:

<% content_for :header do %>
  <script>
    jQuery(function($){
      //alert("ewe");
      $(".date_pick").datepicker();
    })
  </script>
<% end %>
<form action="show_money" methods="get">
  <input type="text" name="start_at" value="<%= (Time.now - 1.year).strftime("%Y/%m/%d") %>" class="date_pick"/>
  <input type="text" name="end_at" value="<%= Time.now.strftime("%Y/%m/%d") %>" class="date_pick"/>

  <select name="level">
    <% @level.each do |level| %>
      <option value="<%= level[1] %>"><%= level[0] %></option>
    <% end %>
  </select>  
  <select name="howuknowu">
    <% @howuknowu.each do |howuknowu| %>
      <option value="<%= howuknowu[1] %>"><%= howuknowu[0] %></option>
    <% end %>
  </select>
  <select name="whylearn">
    <% @whylearn.each do |whylearn| %>
      <option value="<%= whylearn[1] %>"><%= whylearn[0] %></option>
    <% end %>
  </select>
  <input type="submit" value="send"/>
</form>

我有請教其它的程式設計師,他大概說了底下的作法,說是那樣就可以在 view 產生一個 陣列,當參數傳給下一個頁面用。但我無法理解具體的作法該是怎麼樣子

form_tag do
          text_field_tag 'xxx_ids[] x 10'
end

接下來是我控制器裏的寫法 #加入學歷的選項 控制器的頁面

def money
  @level = Incomelevel.all.map{|im|[im.reason_desc , im.id]}    #test ok by rails c
  @howuknowu = Howuknowu.all.map{|sh|[sh.reason_desc , sh.id]}  #test ok by rails c 
  @whylearn = Whylearn.all.map{|sw|[sw.reason_desc , sw.id]}    #test ok by rails c
  render :layout =>"test_layout"
end

def show_money    
  start_at = Time.parse(params[:start_at])
  end_at = Time.parse(params[:end_at])
  level = params[:level].to_i
  howuknowu = params[:howuknowu].to_i
  whylearn = params[:whylearn].to_i
  @income_ids = params['level[][]'].map{|i|i.to_i > 0 ? i.to_i : nil}.compact
  @howuknowus_ids = params['howuknowu[][]'].map{|i|i.to_i > 0 ? i.to_i : nil}.compact
  @whyulearn_ids = params['Whylearn[][]'].map{|i|i.to_i > 0 ? i.to_i : nil}.compact
  @students = Student.from('students AS s').joins("
  INNER JOIN student_incomes AS si ON si.student_id = s.id AND si.income_id IN (#{@income_ids.join(',')})
  INNER JOIN student_howyouknowus AS sh ON sh.student_id = s.id AND sh.howyouknowus_id IN (#{@howuknowus_ids.join(',')})
  INNER JOIN student_whyyoulearn AS sw ON sw.student_id = s.id AND sw.whyyoulearn_id IN (#{@whyulearn_ids.join(',')})  
  INNER JOIN students ON (students.id = si.student_id AND 
      students.created_at BETWEEN 
      DATE('#{start_at.strftime("%Y/%m/%d")}') AND
      DATE('#{end_at.strftime("%Y/%m/%d")}'))
 ")

執行的話就噴出底下的錯誤訊息,求高手指點 錯誤訊息的頁面

"1".map, 你传过来的是一个字符串啊

需要 登录 后方可回复, 如果你还没有账号请 注册新账号