【问题标题】:Radio buttons using simple_form on a has_many through association specifically on the belongs_to side of the association单选按钮在 has_many 上使用 simple_form 通过关联,特别是在关联的 belongs_to 端
【发布时间】:2012-08-06 07:24:48
【问题描述】:

我试图弄清楚如何在我的预约表格中将医生列表作为 radio_buttons。现在,如果我使用“f.input :physician_id, :as => :radio_buttons”,我将得到一个“是/否”单选按钮。如果我更改为“f.association :physician_id, :as => :radio_buttons”,则会收到 RuntimeError “Association :user_id not found”。

顺便说一句,我正在为此使用 simple_form。

谢谢。

我的预约表格:

<%= simple_form_for(@appointment) do |f| %>
  <div class="form-inputs">
    <%= f.hidden_field :patient_id %>
    <%= f.input :physician_id, :as => :radio_buttons %>
    <%= f.hidden_field :appointment_date, :value => DateTime.now  %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Create Appointment" %>
  </div>
<% end %>

我的模型:

/app/models/physician.rb

class Physician < ActiveRecord::Base
  has_many :appointments
  has_many :patients, :through => :appointments

  attr_accessible :physician_name
end

/app/models/appointment.rb

class Appointment < ActiveRecord::Base
  belongs_to :physician
  belongs_to :patient

  attr_accessible :physician_id, :patient_id, :appointment_date, :state
end

/app/models/patient.rb

class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :physicians, :through => :appointments

  attr_accessible :patient_name
end

我的控制器:

/app/controllers/appointment_controller.rb

class AppointmentsController < ApplicationController
  def new
    @appointment = Appointment.new
    @appointment.patient_id = params[:patient_id]
  end

  def create
    @appointment = Appointment.new(params[:appointment])

    if @appointment.save
      flash[:notice] = "New appointment record created"
      redirect_to dashboards_path
    else
      render 'new'
    end
  end
end

【问题讨论】:

  • 试试f.association :physician, :as =&gt; :radio_buttons

标签: ruby-on-rails simple-form


【解决方案1】:

您应该将关联名称传递给association 方法

f.association :physician, :as => :radio_buttons

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 2011-04-30
    相关资源
    最近更新 更多