問題描述
自定義屬性 select_year 助手或使用 javascript 滾動我自己 (custom attribute select_year helper or roll my own with javascript)
I am writing a rails view. It is a form the collects credit card details. It uses braintree.js client side encryption to ensure that the credit card details are encrypted before they leave the client machine. (the Credit card details are not are not part of the model)
However for the encryption to work the "name" attribute needs to be changed to "data‑encrypted‑name" similar to the following.
<div class="field">
<label>Card Number</label>
<input type="text" size="20" autocomplete="off" data‑encrypted‑name="number" />
</div>
I have no problem with the Credit card field...however with the expiry year field I am currently using a select_year helper because it populates the right list of options. However I dont think it is possible to add a custom attribute when using select_year helper...
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+10}, {name: 'year', id: "card_year"}%>
Is JavaScript the best alternative way to create a select input dynamically populates with the right options and has the 'data‑encrypted‑name' attribute?
參考解法
方法 1:
The following should work:
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+10}, {name: 'year', id: "card_year", data: { "encrypted‑name" => "expiry_year" }} %>
That's how you add "data" fields to form helpers.