当用户在常驻显示页面上单击“添加附件”时,我想自动填写常驻表单上的字段。我们已经知道他们想添加附件的居民。
在显示页面按钮上,我传递了常驻参数,并且这些参数在URL形式中可见。尽管该字段尚未完成。
http://localhost:3000/app/attachments/new?resident_id=2
驻留页面上的链接:
<a href="<%= new_app_attachment_path(resident_id: @resident.id) %>" class="btn btn-block btn-default btn-sm">New attachment</a>
居民模型:
class Resident < ApplicationRecord
acts_as_paranoid
has_many :appointments, dependent: :destroy
has_many :care_plans, dependent: :destroy
has_many :contacts, dependent: :destroy
has_many :incidents, dependent: :destroy
has_many :letters, dependent: :destroy
has_many :sonas, dependent: :destroy
has_many :tasks, dependent: :destroy
has_many :activities, dependent: :destroy
has_many :progress_notes, dependent: :destroy
has_many :diagnosis_paragraphs, dependent: :destroy
has_many :attachments, dependent: :destroy
belongs_to :room, optional: true
validates :first_name, presence: true, length: { maximum: 50 }
validates :last_name, presence: true, length: { maximum: 50 }
validates :date_of_birth, presence: true
def full_name
[first_name, middle_name, last_name].select(&:present?).join(' ').titleize
end
end
附件表格:
<%= simple_form_for([:app, @attachment]) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.association :resident, label_method: :full_name, prompt: "Choose a resident", collection: Resident.order(:first_name) %>
<%= f.input :name %>
<%= f.input :document %>
<div class="form-group">
<label>Author</label>
<input class="form-control" type="text" placeholder="<%= @current_user.full_name %>" readonly value="<%= @attachment.author.try(:full_name) %>">
</div>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
答案 0 :(得分:0)
在attaachments#new
动作中,捕获params
并按如下所示设置 resident
def new
@resident = Resident.find(params[:resident_id])
end
现在,像这样将值设置为selected
<%= f.association :resident, label_method: :full_name, prompt: "Choose a resident", collection: Resident.order(:first_name), selected: @resident.id %>