2016-11-07 46 views
1

我有簡單的html.erb形式像下面Rails的ERB一個text_field_tag的獲得價值

<table> 
    <%= text_field_tag :tesintg %> 
    <th><%= button_to 'Apply now', apply_product_index_path(:value => "Want Value of input in text field here") , method: :post %></th> 
</table> 

當「立即申請」按鈕被按下我想在測試text_field_tag價值被張貼查詢參數爲{ 「值」:「文本字段中的值」}

如何實現此目的。

+0

是否有某些原因,你不能將這些元素包裝在一個表單中,讓它爲你完成工作?這將是最簡單的解決方案。否則,你需要使用JavaScript。 – pdoherty926

+0

是的,只需要使用傳統的html表單就可以了。或者,您可以使用JavaScript從文本字段獲取值並將其注入查詢參數。 –

+0

肖恩如何做到這一點。我如何使用javascript來獲取值並在button_to行中使用它。 – user27111987

回答

1

我想做些事情的最好方法一樣,只是創建form_tag

<%= form_tag apply_product_index_path, method: :post do %> 
    <%= text_field_tag :teasing %> 
    <%= submit_tag %> 
<% end %> 

這將傳遞到您的控制器哈希params: { teasing: 'value passed as teasing}。您可以從那裏輕鬆使用params[:teasing]

您不需要從text_field_tag獲取值並將其存入button

另外請記住,如果你正在創建新的對象,非常首選的方法是使用form_for標籤,它使用特定的模型。我不確定你的意圖是什麼,所以我不會重寫已經說過的一切。你可以在這裏閱讀更多:http://guides.rubyonrails.org/form_helpers.html