I have two models Player
and MicroReport
. MicroReport
is a nested resource of Player
. I'm trying to create a separate form to allow users to create a MicroReport
without having to first navigate to a Player's page and then create the report. Is this possible?
I'm was going to try and use the form below, where they could select a player during the creation of the form - but to my knowledge I'd need to specify a player record here (which it currently doesn't exist).
<%= simple_form_for [@micro_report.player, @micro_report] do |form| %>
<div class="row">
<div class="col-xs-12">
<%= form.input :author_id, as: :hidden, input_html: { value: current_user.id } %>
<%= form.input :player, input_html: { class: "player-search-box-for-micro-report" },
data: { autocomplete_source: auto_complete_searches_path } %>
<%= form.input :grade, collection: Grade.joins(:scale)
.where(scales: { name: "Skill Scale" } ) %>
<%= form.input :summary, label: "Summary" %>
</div>
</div>
<div class="form-actions">
<%= form.button :submit %>
</div>
<% end %>
class Player < ApplicationRecord
has_many :micro_reports
end
class MicroReport < ApplicationRecord
belongs_to :author
belongs_to :player
belongs_to :grade
end
routes.rb
resources :players do
scope module: :players do
resources :micro_reports
end
end
In that case you need to add optional: true
to the belongs_to :player
.
belongs_to :player, optional: true
From the Documentation
required
is set to true by default and is deprecated. If you don't want to have association presence validated, useoptional: true
.
With this setup in place, you can create MicroReport
records without adding Player
records on creation.