simple_form 参数 ruby 在 rails 上;自动填充常驻变量

simple_form params ruby on rails; autofill resident variable

当用户从常驻显示页面单击 "add attachment" 时,我想自动完成表单上常驻的字段。我们已经知道他们想要添加附件的居民。

在显示页面按钮上,我传递了常驻参数,这些参数以 url 的形式可见。虽然场没有完成。

http://localhost:3000/app/attachments/new?resident_id=2

Link 在常驻表演页面上:

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

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