2

I'm trying to figure out how can I have list of physicians in my appointment form as radio_buttons. Now if I use "f.input :physician_id, :as => :radio_buttons" I'm getting a "Yes/No" radio buttons. And if I change to "f.association :physician_id, :as => :radio_buttons", then I'm getting RuntimeError "Association :user_id not found".

By the way, i'm using simple_form for this.

Thanks.

My Appointment 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 %>

My models:

/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

My Controllers:

/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
4

1 回答 1

3

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

f.association :physician, :as => :radio_buttons
于 2012-08-09T14:09:06.397 回答