gpt4 book ai didi

ruby-on-rails - 通过关联在 has_many 上使用 simple_form 的单选按钮,特别是在关联的belongs_to 一侧

转载 作者:行者123 更新时间:2023-12-04 05:36:23 26 4
gpt4 key购买 nike

我想弄清楚如何在我的预约表格中将医生列表作为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

我的 Controller :

/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

最佳答案

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

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

关于ruby-on-rails - 通过关联在 has_many 上使用 simple_form 的单选按钮,特别是在关联的belongs_to 一侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11877153/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com