简体   繁体   English

带有嵌套对象的 Rails 6 复杂表单 collection_select 不起作用

[英]Rails 6 Complex Form with nested objects collection_select not working

Would be super grateful if anyone had any insights into what I should be doing that I'm not already doing.如果有人对我应该做但我还没有做的事情有任何见解,我将不胜感激。

db/schema.rb数据库/模式.rb

  create_table "users", force: :cascade do |t|
    t.string "first_name"
    t.string "last_name"
    t.string "email"
    t.string "password_digest"
    t.bigint "cell_number"
    t.boolean "sms_daily"
    t.string "initials"
    t.string "remember_token"
    t.boolean "admin", default: false
    t.boolean "vacation_mode", default: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "remember_digest"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["remember_token"], name: "index_users_on_remember_token"
  end

  create_table "schedules", force: :cascade do |t|
    t.text "comments"
    t.integer "author_id", null: false
    t.boolean "draft", default: true
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["author_id"], name: "index_schedules_on_author_id"
  end

  create_table "rooms", force: :cascade do |t|
    t.integer "schedule_id", null: false
    t.integer "order"
    t.string "site"
    t.string "note"
    t.string "name"
    t.integer "start_hour"
    t.integer "start_minute"
    t.string "user_initials"
    t.boolean "block"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["schedule_id"], name: "index_rooms_on_schedule_id"
  end

app/model/schedule.rb应用程序/模型/schedule.rb

class Schedule < ApplicationRecord
  belongs_to :author, class_name: "User"
  has_many :rooms
  accepts_nested_attributes_for :rooms
  ...
end

app/model/room.rb应用程序/模型/room.rb

class Room < ApplicationRecord
  belongs_to :schedule
  ...
end

app/views/schedule/new.hthl.erb应用程序/视图/时间表/new.hthl.erb

<% provide(:title, 'Create Schedule') %>
<h1>Create Schedule</h1>
<div class="row">
  <aside class="span4">
    <section>
      <%= form_with(model: @schedule, local: true) do |f| %>

        <div class="field">

          <%= f.fields_for :rooms, html: {class: 'form-inline' } do |r| %>

            <div class="room-title" id="<%="#{r.object[:site]}-#{r.object[:name].delete(' ')}" unless r.object[:name] == nil %>">
              <%= "#{r.object[:site]} #{r.object[:name]}"%>
              <%= r.hidden_field :site, value: r.object[:site] %>
              <%= r.hidden_field :name, value: r.object[:name] %>
              <% unless (r.object[:name] == "CHARGE" || r.object[:name] == "CO-CHARGE" || r.object[:site] == "ON_CALL" || r.object[:site] == "On-Call TODAY (Sunday or Holiday)") %>
                <br/>
                <%= r.label :start_hour %>
                <%= r.number_field :start_hour, in: 0..24, step: 1 %>
                <br/>
                <%= r.label :start_minute %>
                <%= r.number_field :start_minute, in: 0..60, step: 1 %>
              <% end %>
              <%= r.collection_select(:user_initials,User.all.map(&:initials).sort.unshift("-- late start").collect,:initials,:initials, include_blank: true) %>
            </div>
          <% end %>
          <%= f.submit "Save Schedule and Review", class: "btn btn-large btn-primary",  input_html: { :tabindex => autotab  } %>
        </div>  
      <% end %>
     </section>
  </aside>
</div>

I'd like for the form to present a choice of all the User's initials to be assigned to the room's user_initials, also offer the choice of not designating anyone's initials with "-- late start" as well as leaving the initials blank for an empty room.我希望该表单可以选择分配给房间的 user_initials 的所有用户首字母,还可以选择不使用“--late start”指定任何人的首字母以及将首字母留空房间。

When I deploy my code to heroku the schedule/new page generates a "We're sorry, something went wrong" error and the logs say:当我将代码部署到 heroku 时,日程表/新页面会生成“我们很抱歉,出了点问题”错误,并且日志显示:

 ActionView::Template::Error (undefined method `initials' for "-- late start":String):

I got this all working in Rails 4 using simple_form but heroku will no longer support the stack that I deployed the site to starting in November, 2020, and heroku's new stack's won't use the old ruby version from the original site.我使用 simple_form 在 Rails 4 中完成了所有工作,但 heroku 将不再支持我从 2020 年 11 月开始部署站点的堆栈,并且 heroku 的新堆栈不会使用原始站点的旧 ruby​​ 版本。 I'm rewriting the code in Rails 6 which simple_form doesn't work for.我在 Rails 6 中重写了 simple_form 不适用的代码。

Thanks in advance for your consideration!预先感谢您的考虑!

collection_select works on a collection of objects and will look for the initials method on each object. collection_select作用于对象集合,并会在每个对象上查找initials方法。

when you do this...当你这样做...

<%= r.collection_select(:user_initials,User.all.map(&:initials).sort.unshift("-- late start").collect,:initials,:initials, include_blank: true) %>

You're just creating an array of strings, and you're prepending another string to the array, and the strings don't have an initials method.您只是在创建一个字符串数组,并且在该数组之前添加另一个字符串,并且这些字符串没有 initials 方法。

Better might be just to use select更好的可能只是使用select

<%= r.select(:user_initials, options_for_select(User.all.pluck(:initials).sort.unshift("-- late start"), r.object.user_initials), include_blank: true) %>

You can use collection_select , it must be ALL user objects, so you could temporarily create a user with initials '--late start', something like...您可以使用collection_select ,它必须是所有用户对象,因此您可以临时创建一个首字母为“--late start”的用户,例如...

<%= r.collection_select(:user_initials, User.order(:initials).to_a.prepend(User.new(initials: "-- late start")), :initials, :initials, include_blank: true) %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM