【问题标题】:Adding A Radio Button To Rails App向 Rails 应用程序添加单选按钮
【发布时间】:2015-05-30 04:08:46
【问题描述】:

我正在尝试在我的“帐户”表单中添加一个单选按钮。 However, when selecting the radio button on the form, it doesn't seem to save in show.html.erb nor in the database.

这是“_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”字段添加到帐户表中。哪个将该字段添加到架构中。

  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)

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

【问题讨论】:

  • 在创建记录时显示日志。 BTW return 是保留的 Ruby 关键字。我不知道这是否导致了问题。让我们看看请给我我的要求。
  • 我认为问题出在您的列名上。您使用了 return 这是一个 ruby​​ 关键字,请使用不同的名称并尝试。例如return_goods 等
  • 上面的表单模板真的有效吗?我认为这个 radio_button 标签应该类似于 &lt;%= f.radio_button :return, true %&gt;
  • @kangkyu,你的回答成功了!对我已经解决了一段时间的问题进行了简单的修复。
  • @curtis,你是对的,当涉及到布尔复选框时,首选。这也是我在逻辑上稍微思考了一下之后得出的结论。这是我的代码“ Returning Client?”的最终结果。这给了我我想要的结果。顺便说一句,我去掉了表单控件类,因为在添加“真”值后,它对该行的格式不正确。

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


【解决方案1】:

radio_button form 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 类型时,您可能需要考虑使用复选框。

【讨论】:

    猜你喜欢
    • 2013-06-10
    • 2018-03-23
    • 2019-09-23
    • 2016-01-13
    • 2019-06-14
    • 1970-01-01
    • 2017-05-11
    • 2011-03-23
    • 2016-08-04
    相关资源
    最近更新 更多