Я пытаюсь создать в своем приложении функцию, которая позволит вам создать occasion
(то есть Рождество 2018 года), а затем выбрать из множества people
, какие из них вам понадобятся, чтобы купить подарки для этого occasion
.
Прямо сейчас у меня есть has_and_belongs_to_many
вот так:
create_table "occasions", force: :cascade do |t|
t.integer "person_id"
t.integer "user_id"
t.string "name"
t.date "date"
t.text "notes"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["person_id"], name: "index_occasions_on_person_id"
t.index ["user_id"], name: "index_occasions_on_user_id"
end
create_table "occasions_people", id: false, force: :cascade do |t|
t.integer "occasion_id", null: false
t.integer "person_id", null: false
end
create_table "people", force: :cascade do |t|
t.string "relationship"
t.string "first_name"
t.string "middle_name"
t.string "last_name"
t.date "birthday"
t.date "anniversary"
t.date "other"
t.string "other_date_name"
t.text "notes"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "group"
t.integer "occasions_id"
t.index ["occasions_id"], name: "index_people_on_occasions_id"
t.index ["user_id"], name: "index_people_on_user_id"
end
Мои отношения заложены как в моей person.rb
модели:
class Person < ApplicationRecord
belongs_to :user
has_and_belongs_to_many :occasions
def full_name
"#{last_name}, #{first_name}"
end
end
, так и в моей occasion.rb
модель:
class Occasion < ApplicationRecord
has_and_belongs_to_many :people
belongs_to :user
end
У меня есть правильные (я думаю) параметры в моих people_controller
:
def person_params
params.require(:person).permit(:relationship, :first_name, :middle_name, :last_name, :birthday, :anniversary, :other, :other_date_name, :notes, :group, :user_id, :occasion_id)
end
И в моих occasions_controller
:
def occasion_params
params.require(:occasion).permit(:person_id, :user_id, :name, :date, :notes)
end
В моей форме есть следующее, чтобы создать новый occasion
как компромисс между этой документацией и этим SO исправлением:
<%= simple_form_for(@occasion) do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<div class="form-inputs">
<div class="row">
<div class="form-group col-sm-6 ">
<%= f.input :name, required: true %>
</div> <!-- col -->
<div class="form-group col-sm-6">
<%= f.label :date %>
<input type="text" class="form-control datepicker" data-datepicker-holder="#dateHolder"/>
<%= f.hidden_field :date, id: "dateHolder", class: "hiddenDateField" %>
</div> <!-- col -->
</div> <!-- row -->
<div class="form-group">
<%= f.label :notes %>
<%= f.text_area :notes, class: "form-control" %>
</div> <!-- col -->
<div class="form-group collection-checkboxes">
<%= f.label "Who do you need to shop for?" %><br />
<%= f.collection_check_boxes(:people, Person.where(user_id: current_user.id).order("last_name ASC"), :id, :full_name) %>
</div> <!-- col -->
</div>
<div class="form-actions text-center">
<%= f.button :submit, class: "btn btn-outline-primary", style: "margin-bottom: 10px" %>
</div>
<% end %>
Когда я пытаюсьсоздайте новый occasion
, флажки отображаются так, как должны, но независимо от того, что отмечено, никакая ассоциация не зарегистрирована, и я получаю unpermitted parameter
для :people
:
Started POST "/occasions" for 127.0.0.1 at 2018-10-06 14:59:05 -0700
Processing by OccasionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"utl5aK6RoJRxGKUsYbrz6tHs4uWzWMROXBMRJaxSqLd00CMHcBZ1L9WondisYvtWYJ2tw4X4QxMKAXSFurPV6g==", "occasion"=>{"name"=>"Test Occasion", "date"=>"2018-12-25", "notes"=>"This is a test. It should have 3 people associated with this occasion.", "people"=>["", "22", "24", "25"]}, "commit"=>"Create Occasion"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ /Users/lizbayardelle/.rvm/gems/ruby-2.5.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
Unpermitted parameter: :people
(0.1ms) begin transaction
↳ app/controllers/occasions_controller.rb:34
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/controllers/occasions_controller.rb:34
Occasion Create (0.6ms) INSERT INTO "occasions" ("user_id", "name", "date", "notes", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["user_id", 1], ["name", "Test Occasion"], ["date", "2018-12-25"], ["notes", "This is a test. It should have 3 people associated with this occasion."], ["created_at", "2018-10-06 21:59:05.931738"], ["updated_at", "2018-10-06 21:59:05.931738"]]
↳ app/controllers/occasions_controller.rb:34
(1.6ms) commit transaction
↳ app/controllers/occasions_controller.rb:34
Redirected to http://localhost:3000/occasions
Completed 302 Found in 10ms (ActiveRecord: 2.9ms)
Может кто-нибудь увидеть, чтоНужно изменить здесь, чтобы заставить его работать как задумано?Я новичок в ассоциациях HABTM.
ОБНОВЛЕННЫЙ ЖУРНАЛ СЕРВЕРА ПОСЛЕ ОРИГИНАЛЬНОГО ОТВЕТА ПАВАНА
Started POST "/occasions" for 127.0.0.1 at 2018-10-14 06:38:41 -0700
Processing by OccasionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"XQ7RkDMBG/U+FECZcZ4A3eTmNgXVdAswbgIBdAErRNKTB4v/7YbOTpqkeG28RghhVZd5I+PUjG04EGTUF8o5jw==", "occasion"=>{"name"=>"Test", "date"=>"2018-10-01", "notes"=>"", "person_ids"=>["", "22", "24", "25"]}, "commit"=>"Create Occasion"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ /Users/lizbayardelle/.rvm/gems/ruby-2.5.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
Person Load (0.2ms) SELECT "people".* FROM "people" WHERE "people"."id" IN (?, ?, ?) [["id", 22], ["id", 24], ["id", 25]]
↳ app/controllers/occasions_controller.rb:30
(0.1ms) begin transaction
↳ app/controllers/occasions_controller.rb:34
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/controllers/occasions_controller.rb:34
CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/controllers/occasions_controller.rb:34
CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/controllers/occasions_controller.rb:34
CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/controllers/occasions_controller.rb:34
Occasion Create (0.5ms) INSERT INTO "occasions" ("user_id", "name", "date", "notes", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["user_id", 1], ["name", "Test"], ["date", "2018-10-01"], ["notes", ""], ["created_at", "2018-10-14 13:38:41.063659"], ["updated_at", "2018-10-14 13:38:41.063659"]]
↳ app/controllers/occasions_controller.rb:34
Occasion::HABTM_People Create (0.2ms) INSERT INTO "occasions_people" ("occasion_id", "person_id") VALUES (?, ?) [["occasion_id", 2], ["person_id", 22]]
↳ app/controllers/occasions_controller.rb:34
Occasion::HABTM_People Create (0.1ms) INSERT INTO "occasions_people" ("occasion_id", "person_id") VALUES (?, ?) [["occasion_id", 2], ["person_id", 24]]
↳ app/controllers/occasions_controller.rb:34
Occasion::HABTM_People Create (0.1ms) INSERT INTO "occasions_people" ("occasion_id", "person_id") VALUES (?, ?) [["occasion_id", 2], ["person_id", 25]]
↳ app/controllers/occasions_controller.rb:34
(1.2ms) commit transaction
↳ app/controllers/occasions_controller.rb:34
Redirected to http://localhost:3000/occasions
Completed 302 Found in 20ms (ActiveRecord: 3.0ms)