Когда пользователь нажимает «добавить вложение» со страницы резидента, я хотел бы автоматически заполнить резидент поля в форме.Мы уже знаем, к какому резиденту они хотели бы добавить вложение.
На кнопке «Показать страницу» я передаю резидентные параметры, и эти параметры отображаются в форме 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 %>