使用 accepts

编程入门 行业动态 更新时间:2024-10-27 00:24:11
本文介绍了使用 accepts_nested_attributes_for 创建表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有 2 个模型,一个用户和一个患者.一个用户有一个患者,一个患者 BELONGS_TO 一个用户.

I have 2 models, a User and Patient. A User HAS_ONE Patient and a Patient BELONGS_TO a User.

class Patient < ActiveRecord::Base belongs_to :user accepts_nested_attributes_for :user attr_accessible :user_id, :user_attributes end # == Schema Information # # Table name: patients # # id :integer not null, primary key # user_id :integer # insurance :string(255) # created_at :datetime # updated_at :datetime # class User < ActiveRecord::Base has_one :patient attr_accessible :username, :password, :active, :disabled, :first_name, :last_name, :address_1, :address_2, :city, :state, :postcode, :phone, :cell, :email attr_accessor :password end # == Schema Information # # Table name: users # # id :integer not null, primary key # username :string(255) # encrypted_password :string(255) # salt :string(255) # active :boolean # disabled :boolean # last_login :time # first_name :string(255) # last_name :string(255) # address_1 :string(255) # address_2 :string(255) # city :string(255) # state :string(255) # postcode :string(255) # phone :string(255) # cell :string(255) # email :string(255) # created_at :datetime # updated_at :datetime #

在我的患者控制器中,我正在尝试创建一个新的患者表单.

In my patients controller I am trying to create a new Patient form.

class PatientsController < ApplicationController def new @patient = Patient.new end end

在我的视图中 (new.html.erb)

In My View (new.html.erb)

<%= form_for @patient do |patient_form| %> <% patient_form.fields_for :user do |user_fields| %> <table class="FormTable" border="0" cellspacing="0" cellpadding="0"> <tr> <td class="label"> <%= user_fields.label :username %> *: </td> <td class="input"> <%= user_fields.text_field :username, :class=>"TextField" %> </td> </tr> </table> ... <%end%> <%end%>

表单显示为空白,带有一个提交按钮,没有为 user_fields 生成标记

The form shows up blank with a submit button with no generated markup for the user_fields

我被告知我这样做是错误的,因为患者有 accepts_nested_attributes_for :user 并且它应该是用户在我的系统中嵌套属性但我想使用资源模型,以便治疗患者和其他用户类型分开.

I have been told I am doing this wrong because of the patient having the accepts_nested_attributes_for :user and it should be the user nesting the attributes BUT in my system I want to use the resources model so a patient and other user types are treated separately.

示例数据库表:

用户:id|first_name|last_name...等

USERS: id|first_name|last_name...etc

患者:id|user_id|保险

PATIENTS: id|user_id|insurance

推荐答案

除非我弄错了,当您调用 fields_for 时,您没有 user.在您可以执行 fields_for 之前,您需要有一个可用于构建表单的用户实例,有点像您的 @patientpatient_form.

Unless I'm mistaken, you have no user when you are calling fields_for. Before you can do the fields_for, you will need to have an instance of a user that can be used to build the form, kind of like how you have @patient for your patient_form.

最好的办法是根据您的 @patient 在您的控制器中构建一个 User,然后您就可以在视图中访问它.

Your best bet would be to build a User in your controller, based off of your @patient, and then you will have access to that in the view.

更多推荐

使用 accepts

本文发布于:2023-10-18 03:10:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502899.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:accepts

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!