[英]How to pass additional (radio-buttons) parameters through rails form created with form_for?
我有一個form_for幫手,可以為我的Image
模型創建一個對象。 看起來像這樣:
<%= form_for :image, url: images_path do |f| %>
<p>
<%= f.file_field :file %>
</p>
<p>
<input type="radio" name="index" value="1">1
<input type="radio" name="index" value="2">2
<input type="radio" name="index" value="3">3
</p>
<p><%= f.submit "submit" %></p>
<% end %>
觀察params哈希后,:file如預期般通過。 我也需要在此哈希中傳遞單選按鈕中的值,或者至少,我需要知道Image控制器的create
函數中的該值是什么。 如何通過params哈希(或其他方式)傳遞此值?
您可以將單選按鈕的name
屬性更改為類似於image[index]
。
更好的方法(IMO)是使用實例變量存儲該值,因為它將允許您編寫類似f.radio_button :index
代碼。
例如
class Image < ActiveRecord::Base
attr_accessor :index
# Uncomment if you're using Rails < 4, otherwise whitelist the attr in the controller
#attr_accessible :index
end
在側面,考慮使用諸如radio_button_tag
類的表單助手,這比純HTML更好。
在form_for中,您可以執行以下操作:
<p>
<%= f.radio_button_tag(:index, "1") %>
<%= f.label_tag(:index_1, "1") %>
<%= f.radio_button_tag(:index, "2") %>
<%= f.label_tag(:index_2, "2") %>
<%= f.radio_button_tag(:index, "3") %>
<%= f.label_tag(:index_3, "3") %>
</p>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.