2016-12-22 42 views
0

我做了Rails中的表:通過值從查看到控制器動作

<table class="table_box"> 
     <%= table_head([ 
      {text: "wanted",  width: "30%"}, 
      {text: "Skill",    width: "30%"}, 
      {text: "Duration (In months)", width: "30%"}, 
      {text: "Proficiency",  width: "10%"}])%> 

    <tbody id="skill"> 
    <% skills.each do |skill| %> 
     <tr class="<%= cycle("tr_box","tr_box odd",:name=>"details")%>"> 
     <% contains_skill = user_skills.include? skill 
      indeex = user_skills.index(skill) if contains_skill 
     has_experience = (indeex ? user_experience[indeex] : '') 
     has_proficiency = (indeex ? user_proficiency[indeex] : '') 
     %> 
     <%= table_row([ 
      {:text=>check_box_tag("checkbox[]",value = "1",checked = contains_skill)}, 
      {:text=>skill}, 
      {:text=> (number_field_tag 'quantity', has_experience, in: 1...5)}, 
      {:text=> (select_tag "credit_card", options_for_select([ '','Expert', 'Average','Beginner' ], has_proficiency))}, 
     ])%> 
    </tr> 
    <% end %> 
     <div class="clear"></div> 
     </tbody> 
    </table> 

現在我希望所有的參數(表值)進入我的控制器,所以我可以處理它們。我試着製作一個按鈕:

<%= button_to "New", action: "update" %> 

但這沒有奏效。 我怎樣才能從表中的值到控制器?

+1

使用表格:http://guides.rubyonrails.org/form_helpers.html –

+0

嘗試過這種形式,但是我的實現不起作用 –

+0

您需要將輸入字段包裝在表單標記中,該標記的行爲指向映射到您的路線控制器行動。另外,使用'submit_tag'而不是'button_to'。 http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-submit_tag –

回答

0

如果表單的複雜程度超過最簡單的表單,那麼簡單的表單寶石可能是一個很大的幫助。

看看這裏:https://github.com/plataformatec/simple_form

它可以讓你創建一個基於模型視圖模型或您的形式。除此之外,它是非常有據可查的。