如何从轨道上的ruby中的复选框中获取值?

时间:2015-05-18 09:12:11

标签: mysql ruby-on-rails ruby

我正在获取布尔值输出。

    **_form.html.erb**


    <%= form_for(@user, :html => {:multipart => true}) do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% @user.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :price %><br />
    <%= f.text_field :price %>
  </div>
  <div class="field">
  Gender:<br>  <%= f.radio_button(:gender, "Male")%>
    <%= f.label(:gender_option1, "Male") %><br />
     <%= f.radio_button(:gender, "Female")%>
    <%= f.label(:gender_option2, "Female") %>
   </div>
  <div class="field">
    Payment:<br>
    <%= f.check_box(:paymentmode )%>
    <%= f.label :Debitcard %>
    <%= f.check_box :paymentmode %>
    <%= f.label :Creditcard %>
    <%= f.check_box :paymentmode %>
    <%= f.label :cashondelivery %>

   <br />


  </div>
  <div class="field">
    <%= f.label :image %><br />
    <%= f.file_field :image %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

    **Index.html.erb**

        <tr>
        <td><%= user.name %></td>
        <td><%= user.email %></td>
        <td><%= user.gender %></td>
        <td>$<%= user.price %></td>
        <td><%= user.paymentmode %></td>
        </tr>

我想获得复选框的价值,即借记卡或信用卡或货到付款。我得到的输出如下:

Name: xyz Email: xyz@gmail.com gender:Male price:$100 payment mode : 0

我需要付款模式,如借记卡或贷记或字符串格式的鳕鱼。我想要付款方式:Cashondelivery/credit/debit.

1 个答案:

答案 0 :(得分:0)

使用radio buttons,就像使用男性或女性一样。它们是相互排斥的。 <%= f.radio_button(:paymentmode, "debit")%> <%= f.label(:debitcard, "Debit Card") %> <%= f.radio_button(:paymentmode, "credit")%> <%= f.label(:dreditcard, "Credit Card") %> <%= f.radio_button(:paymentmode, "Cash")%> <%= f.label(:cash, "Cash on delivery") %>