Simple_Form pars ruby on rails;自动填充常驻变量 [英] simple_form params ruby on rails; autofill resident variable

查看:11
本文介绍了Simple_Form pars ruby on rails;自动填充常驻变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户在Residence显示页面中单击"添加附件"时,我希望在表单上自动填写"Resident"字段。我们已经知道他们想要添加附件的居民。

在显示页面按钮上,我传递驻留参数,这些参数在表单url中可见。尽管这块地还没有完工。

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

Residient Show页面链接:

<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并设置驻留,如下

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

这篇关于Simple_Form pars ruby on rails;自动填充常驻变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆