添加单选按钮到Rails应用程序

时间:2015-05-29 19:07:36

标签: ruby-on-rails ruby-on-rails-4

我正在尝试在“帐户”表单中添加一个单选按钮。但是,在表单上选择单选按钮时,它似乎不会保存在show.html.erb中,也不会保存在数据库中。

这是“_form.html.erb”

    <%= form_for(@account) do |f| %>
      <% if @account.errors.any? %>
        <div id="error_explanation">

          <h2><%= pluralize(@account.errors.count, "error") %> prohibited this account from being saved:</h2>

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

      <div class="form-group">
        <%= f.label :first_name %><br>
        <%= f.text_field :first_name, class: "form-control" %>
      </div>
      <div class="form-group">
        <%= f.label :last_name %><br>
        <%= f.text_field :last_name, class: "form-control" %>
        <div class="form-group">

        <%= f.label :return %><br>
        <%= f.radio_button :return,  class: "form-control" %> Returning Client?

        <div class="form-group">
        <%= f.label :program %><br>
        <%= f.collection_select :program_id, Program.all, :id, :program, {prompt: "Choose a Program"}, {class: "btn btn-default dropdown-toggle"} %>
      </div>
      <div class="form-group">
        <%= f.label :address %><br>
        <%= f.text_field :address, class: "form-control" %>
      </div>
      <div class="form-group">
        <%= f.label :phone %><br>
        <%= f.text_field :phone, class: "form-control" %>
      </div>
      <div class="actions">
        <%= f.submit class: "btn btn-primary" %>
      </div>
    <% end %>


<!-- end snippet -->

Accounts_Controller

 class AccountsController < ApplicationController
      before_action :authenticate_user!
      before_action :set_account, only: [:show, :edit, :update, :destroy]

      respond_to :html

      def index
        @account = Account.all
        respond_with(@account)
      end

      def show
        @notes = Note.where(account_id: @account.id) #Where a note belong to the current account
      end

      def new
        @account = Account.new
        respond_with(@account)
      end

      def edit
      end

      def create
        @account = Account.new(account_params)
        @account.save
        respond_with(@account)
      end

      def update
        @account.update(account_params)
        respond_with(@account)
      end

      def destroy
        @account.destroy
        respond_with(@account)
      end

      private
        def set_account
          @account = Account.find(params[:id])
        end

        def account_params
          params.require(:account).permit(:first_name, :last_name, :return, :program_id, :address, :phone)
        end
    end

我创建的迁移文件,用于将“return”字段添加到accounts表中。这将字段添加到架构中。

  class AddReturnToAccounts < ActiveRecord::Migration
  def change
    add_column :accounts, :return, :boolean, default: false
  end
end

Rails服务器的输出

 Parameters: {"id"=>"1"}
  User Load (0.2ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
  Account Load (0.1ms)  SELECT  "accounts".* FROM "accounts"  WHERE "accounts"."id" = ? LIMIT 1  [["id", 1]]
  Program Load (0.1ms)  SELECT  "programs".* FROM "programs"  WHERE "programs"."id" = ? LIMIT 1  [["id", 1]]
  Note Load (0.2ms)  SELECT "notes".* FROM "notes"  WHERE "notes"."account_id" = 1
  User Load (0.1ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Rendered accounts/show.html.erb within layouts/application (4.2ms)
  Rendered layouts/_bootstrap.html.erb (0.1ms)
  Rendered layouts/_bootstrap.html.erb (0.1ms)
  Rendered layouts/_navbar.html.erb (0.5ms)
  Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 82ms (Views: 79.2ms | ActiveRecord: 0.7ms)

我不确定价值是否正确传递,我想这就是我所缺少的但我不确定。

1 个答案:

答案 0 :(得分:0)

radio_button form helper也将值作为参数。所以,现在的方式是,helper方法认为哈希值({class: "form-control"})是值。渲染的单选按钮看起来像:

<input type="radio" value="{:class=>&quot;form-control&quot;}" name="account[return]" id="account_return_classform-control"> Returning Client?

如果选中单选按钮,则传入要保留的值:

<%= f.radio_button :return, true, class: "form-control" %> Returning Client?

这将为您提供您期望的渲染输入:

<input class="form-control" type="radio" value="true" checked="checked" name="account[return]" id="account_return_true">

但是,对于boolean类型,您可能需要考虑使用复选框。