Create parent and child entry from same nested forms in Rails - ruby-on-rails

EDIT 3: Just to clarify, the goal and problem is to create 2 new records from the same form of which one is the parent and one is the child. The child needs the parent ID, but the parent is created from the same form that the child is.
EDIT 2: I think I'm getting closer. See log file at end, the deal is successfully saved and it looks like the client entry is starting to commit but then not saving. Code is updated below for changes.
I followed the Railscast #196 for nested forms and I am successfully able to edit, add and delete from nested forms as long as the record is already created. Now I am trying to use nested forms to create new records. I think I'm 99% of the way there, but I'm missing something I can't see anymore. I just need to figure out how to pass the id of the parent to the child.
In addition to the Railscast I used this answer to set inverse_of relationships and call the save order (using create instead of save though). I'm pretty sure the problem is in the form or the controller (but models are listed below too)
Nested Form (I tried to simplify to make it easier to read)
EDIT 2: remove hidden field
<%= form_for(#deal) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="deal-<%= #deal.id %>" >
<div class="form-group">
<%= f.label :headline %>
<%= f.text_field :headline, required: true, placeholder: "Headline" %>
</div>
<div class="form-group" id="clients">
<%= f.fields_for :clients do |client_form| %>
<div class="field">
<%= client_form.label :client %><br />
<%= client_form.text_field :name, placeholder: "Client name" %>
</div>
<% end %>
<%= link_to_add_fields "Add client", f, :clients %>
</div>
<div class="form-group">
<%= f.label :matter %>
<%= f.text_field :matter, placeholder: "Matter", rows: "4" %>
</div>
<div class="form-group">
<%= f.label :summary %>
<%= f.text_area :summary, placeholder: "Deal summary", rows: "4" %>
</div>
<div class="form-group">
<div class="action-area">
<%= f.submit "Add deal" %>
</div>
</div>
</div>
<% end %>
Controller
EDIT 2: include deal_id param & change save calls
class DealsController < ApplicationController
before_action :require_login
def new
#deal = Deal.new
#client = #deal.clients
end
def create
#deal = current_user.deals.create(deal_params)
if #deal.save
flash[:success] = "Your deal was created!"
redirect_to root_url
else
render 'deals/new'
end
end
private
def deal_params
params.require(:deal).permit(:headline, :matter, :summary, clients_attributes: [:id, :deal_id, :name, :_destroy])
end
end
EDIT 2: No longer yields errors in browser and success flash message is triggered
EDIT 2: Here is the console output on submit (the record is saved and can be viewed it just doesn't have a client)
Started POST "/deals" for ::1 at 2017-04-26 00:13:08 +0200
Processing by DealsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"hbvpS6KsZOorR3u4LgNoG5WHgerok6j3yYzO+dFUHs9thsxRi+rbUkm88nb7A5WvlmWZEcvaDvCKywufP3340w==", "deal"=>{"headline"=>"headline", "client"=>{"name"=>"aaa"}, "matter"=>"", "summary"=>""}, "commit"=>"Add deal"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
Unpermitted parameter: client
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "deals" ("headline", "matter", "summary", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["headline", "headline"], ["matter", ""], ["summary", ""], ["user_id", 1], ["created_at", 2017-04-25 22:13:08 UTC], ["updated_at", 2017-04-25 22:13:08 UTC]]
(3.8ms) commit transaction
(0.1ms) begin transaction
(0.1ms) commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 16ms (ActiveRecord: 4.6ms)
Models for reference
Users
class User < ApplicationRecord
has_many :deals
end
Deals
class Deal < ApplicationRecord
belongs_to :user
has_many :clients, inverse_of: :deal
validates :headline, presence: true
accepts_nested_attributes_for :clients, allow_destroy: true
end
Clients
class Client < ApplicationRecord
belongs_to :deal, inverse_of: :clients
validates :name, presence: true
validates :deal_id, presence: true
end

You are missing the deal_id on the params
def deal_params
params.require(:deal).permit(:headline, :matter, :summary, clients_attributes: [:id, :deal_id, :name, :_destroy])
end
and on the create of deal, you can make something like this
def create
#deal = Deal.new(deal_params)
if #deal.save
...
and just add a hidden field on the form for the user_id parameter.

The problem is the validations in the client model. Removing the validations will allow the records to be correctly saved from the nested form.
In the Client model remove the name and deal_id validators
class Client < ApplicationRecord
belongs_to :deal, inverse_of: :clients
end
In the controller add build to the new action so it's nicer for the users:
def new
#deal = Deal.new
#client = #deal.clients.build
end
.build is not strictly necessary since the helper method that was created from the Rails Cast will create new client entries on demand, but with it the user is presented with the placeholder first entry which is ignored if blank.
To keep empty client records from saving I added a reject_if: proc to the deal model
class Deal < ApplicationRecord
belongs_to :user
has_many :clients, inverse_of: :deal
validates :headline, presence: true
accepts_nested_attributes_for :clients, allow_destroy: true, reject_if: proc { |attributes| attributes['name'].blank? }
end
I'll leave this open/unanswered in case someone can explain better why my solution worked and if there's a better way to do it.

Related

Unpermitted parameters for has_many :through nested form in Rails 5

Hi I am trying to implement a has_many :through nested form in my Rails 5 app to create a project with users assigned to it. However, when I try to create the project, I face the following validation errors:
- Project users user company must exist
- Project users user email can't be blank
- Project users user email is invalid
- Project users user password can't be blank
- Project users user firstname can't be blank
- Project users user lastname can't be blank
These are validations I did for the user model but I do not understand why they appear when I create a project with nested users. In addition, the console indicated :fullname as an unpermitted parameters, as shown below.
Parameters: {"utf8"=>"✓", "authenticity_token"=>"3RtrzKKVrSOiGLYnHwubF3GaRAbcQE/61doRM5clT8GpFYeJoEVnU2lEmQsNwVO8qecdqig8xjwbTqqaqL1gYQ==", "project"=>{"name"=>"dsada", "description"=>"dadasd", "project_users_attributes"=>{"0"=>{"user_attributes"=>{"fullname"=>["", "5", "4", "3"]}}},"choice"=>"1", "button"=>"", "company_id"=>"1"}
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Company Load (0.3ms) SELECT "companies".* FROM "companies" WHERE "companies"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Unpermitted parameter: fullname
Could anyone enlighten me on why I am facing these validation errors and why fullname is considered unpermitted although i included them in project strong params? My codes are as shown below. Thank you!
#Model - project.rb
class Project < ApplicationRecord
has_many :project_users, inverse_of: :project
has_many :users, through: :project_users
accepts_nested_attributes_for :project_users
end
#Model - project_user.rb
class ProjectUser < ApplicationRecord
belongs_to :project, inverse_of: :project_users
belongs_to :user, inverse_of: :project_users
accepts_nested_attributes_for :user
end
#Model - user.rb
class User < ApplicationRecord
has_many :project_users, inverse_of: :user
has_many :projects, through: :project_users
end
#Controller - projects_controller.rb
class ProjectsController < ApplicationController
def new
#company = Company.find(params[:company_id])
#project = #company.projects.build
#project.project_users.build.build_user
end
def create
#company = Company.find(params[:company_id])
#project = #company.projects.build(proj_params)
if #project.valid?
#project.save
render 'show'
else
render 'new'
end
end
private
def proj_params
params.require(:project).permit(:name, :description, project_users_attributes: [:id, user_attributes:[:fullname]])
end
end
end
#Views - new.html.erb
<%= form_for [#company, #project] do |f| %>
<%= f.label :title %>
<%= f.text_field :name %>
<%= f.label :description %>
<%= f.text_area :description %>
<%= f.fields_for :project_users do |project_user_form| %>
<%= project_user_form.fields_for :user do |user| %>
<%= user.label :team_members %>
<%= user.select :fullname, company.users.collect { |p| [ p.fullname, p.id ] }, {:prompt => 'Select Team Members'}, { :multiple => true} %>
<% end %>
<% end %>
<%= f.button :submit => "" %>
<% end %>
Here is why your strong parameters does not work well.
You're saying in proj_params that :fullname is a field. But it's not just a field - it's an array of values. So if you change that to :fullname => [] that'll permit the parameter.
But.. that doesn't make any sense.
Your code is trying to create new users, not assign them. That's why you get validations errors. The simplest way to prevent that is to pass IDs of user, which will try to update / build association, not create.
Then, you'd be better off to pass users' IDs from the view. Try passing user_ids instead of fullnames and don't forget user_ids => [] in strong parameters.

How to get this nested form to create a new object

My Rails 5 application includes two models, Activity and Timeslot.
activity.rb
class Activity < ApplicationRecord
belongs_to :club
has_many :timeslots, :dependent => :destroy
accepts_nested_attributes_for :timeslots
validates :club_id, presence: true
validates :name, presence: true, length: { maximum: 50 }
end
timeslot.rb
class Timeslot < ApplicationRecord
belongs_to :activity
validates :time_start, presence: true
validates :time_end, presence: true
validates :day, presence: true
#validates :activity_id, presence: true (I realised this was causing one of the errors I had)
default_scope -> { order(day: :asc) }
end
When I create my activity, I'd also like to create it's first timeslot on the same page, same form.
new.html.erb
<%= form_for(#activity) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.label :name, "Class name" %>*
<%= f.text_field :name, class: 'form-control' %>
<%= f.fields_for :timeslots do |timeslots_form| %>
<%= timeslots_form.label :time_start, "Start time" %>
<%= timeslots_form.time_select :time_start %>
<%= timeslots_form.label :time_end, "End time" %>
<%= timeslots_form.time_select :time_end %>
<%= timeslots_form.label :day %>
<%= timeslots_form.select :day, (0..6).map {|d| [Date::DAYNAMES[d], d]} %>
<% end %>
</div>
<%= f.submit "Create class", class: "btn btn-primary" %>
<% end %>
My edit/update version of this seems to be working fine.
activities_controller.rb
class ActivitiesController < ApplicationController
...
def new
#activity = Activity.new
#activity.timeslots.build
end
def create
#activity = current_club.activities.build(activity_params)
##activity.timeslots.first.activity_id = #activity.id (I thought this might solve the problem, but didn't)
if #activity.save
flash[:success] = "New class created!"
redirect_to activities_path
else
render 'new'
end
end
def edit
#activity = current_club.activities.find_by(id: params[:id])
#activity.timeslots.build
end
def update
#activity = current_club.activities.find_by(id: params[:id])
if #activity.update_attributes(activity_params)
flash[:sucess] = "Class updated!"
redirect_to edit_activity_path(#activity)
else
render 'edit'
end
end
...
private
def activity_params
params.require(:activity).permit(:name, :active, #active is set to default: true
:timeslots_attributes => [:id,
:time_start,
:time_end,
:day,
:active])
end
end
But whenever I try to create a new activity I get the error message "Timeslots activities must exist".
I feel it's trying to assign the activity_id for timeslot before the activity is created, but I'm not sure. I've tried many things (some of which I've included in my example in comment form) but am unable to work out why I'm getting this error.
Update: Add error log
Started POST "/activities" for 127.0.0.1 at 2016-09-25 18:04:51 +0700
Processing by ActivitiesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xp+dBcWC4cjI6FLpIqhU0RzM4ldZ4JpkFLSyAXcmifL73QqWz6R65EHm/Tj7QxlXnWiBA0axjVXvMZHQ+XKA9A==", "activity"=>{"name"=>"Newest class", "timeslots_attributes"=>{"0"=>{"time_start(1i)"=>"2016", "time_start(2i)"=>"9", "time_start(3i)"=>"25", "time_start(4i)"=>"12", "time_start(5i)"=>"00", "time_end(1i)"=>"2016", "time_end(2i)"=>"9", "time_end(3i)"=>"25", "time_end(4i)"=>"13", "time_end(5i)"=>"30", "day"=>"4"}}}, "commit"=>"Create class"}
Club Load (0.2ms) SELECT "clubs".* FROM "clubs" WHERE "clubs"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
(0.1ms) begin transaction
(0.1ms) rollback transaction
Rendering activities/new.html.erb within layouts/application
Rendered shared/_error_messages.html.erb (1.5ms)
Rendered activities/new.html.erb within layouts/application (16.9ms)
Rendered layouts/_rails_default.html.erb (58.5ms)
Rendered layouts/_shim.html.erb (0.5ms)
Rendered layouts/_header.html.erb (1.7ms)
Rendered layouts/_footer.html.erb (0.8ms)
Completed 200 OK in 111ms (Views: 93.9ms | ActiveRecord: 0.3ms)
You're getting this error because Rails 5 makes belongs_to attribute required by default. Also, the saving mechanism kind of follows:
validate your parent model
validate your child model # validation fails here because parent doesn't have an id yet, because it hasn't been saved
save parent model
save child model
A way to resolve this is to:
class Activity < AR
has_many :timeslots, dependent: :destroy, inverse_of: :activity
accepts_nested_attributes_for :timeslots
end
class Timeslot < AR
belongs_to :activity, inverse_of: :timeslot
end
You could learn more of this here and here

Rails 4 Nested Forms, Error: Unpermitted parameter: order_item

My goal is to create a new Order and an associated OrderItem using the same form.
Models
class Order < ActiveRecord::Base
belongs_to :user
has_many :order_items, dependent: :destroy
accepts_nested_attributes_for :order_items
validates_associated :order_items
end
class OrderItem < ActiveRecord::Base
belongs_to :order
default_scope -> { order(created_at: :desc) }
end
View
<% #items.each do |item| %>
<%= form_for(#order) do |f| %>
<%= f.hidden_field :user_id, :value => session[:user_id] %>
<%= f.fields_for :order_items do |oi| %>
<%= oi.hidden_field :product_id, :value => item.id %>
<%= oi.hidden_field :price, :value => item.price %>
<%= oi.number_field :quantity, value: 1, class: 'form-control', min: 1 %>
<% end %>
<%= f.submit "Buy Now", class: "btn btn-primary" %>
<% end %>
Controller
def new
#order = Order.new
#order.order_items.build
end
def create
#order = Order.new(order_params)
if #order.save
redirect_to cart_path
else
redirect_to root_url
end
end
private
def order_params
params.require(:order).permit(:user_id, :custom_item_id, order_items_attributes: [:product_id, :price, :quantity])
end
When submitting the nested form data to the database, the error message Unpermitted parameter: order_item gets returned and only the order data is saved.
Update <-- This is resolved
When I remove the "f." from <%= f.fields_for the form renders correctly and order_params includes the order_items data. This is interesting because the RailsGuide for Form Helpers includes the "f." http://guides.rubyonrails.org/form_helpers.html#nested-forms
Parameter
{"utf8"=>"✓", "authenticity_token"=>"<TOKEN>", "order"=>{"user_id"=>"1", "order_item"=>{"product_id"=>"5", "price"=>"120.0", "quantity"=>"1"}}, "commit"=>"Buy Now"}
The data still does not save to the corresponding models.
Update 2 <-- This is resolved
Updated the createaction in the controller to if #order.save!, below is the error message:
Validation failed: Order items order can't be blank, Order items product can't be blank, Order items quantity can't be blank, Order items price can't be blank, Order items is invalid
I believe that the mistake is in this line of code #order.order_items.build(order_params[:order_items_attributes]) but I am not sure what I need to change.
Update 3 Unpermitted parameter: order_item Error message
From the terminal:
Processing by OrdersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=><TOKEN>, "order"=> "user_id"=>"1", "order_item"=>{"product_id"=>"5", "price"=>"120.0", quantity"=>"1"}}, "commit"=>"Buy Now"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Unpermitted parameter: order_item
(0.2ms) begin transaction
SQL (1.4ms) INSERT INTO "orders" ("user_id", "created_at", "updated_at") VALUES (?, ?, ?) [["user_id", 1], ["created_at", "2016-03-18 14:58:21.724246"], ["updated_at", "2016-03-18 14:58:21.724246"]]
(14.4ms) commit transaction
The order_itemsdata does not get saved.
First, it seems like your association is incorrect in OrderItem. It should be belongs_to :order instead of belongs_to :order_item_params.
Second, I believe your form should say <%= f.fields_for :order_items do |oi| %> (:order_items not :order_item)
Finally, you should not need to do this in your controller: #order.order_items.build(order_params[:order_items_attributes])
Controller
def create
#order = Order.new(order_params)
if #order.save
redirect_to cart_path
else
redirect_to root#url
end
end
def order_params
params.require(:order).permit(:user_id, :custom_item_id, order_items_attributes: [:product_id, :price, :quantity])
end
The view that rendered the form was not in another controller and not in the orders controller. The show action of tht view did not include an instance variable for order_items. This caused the error messages. Thank you for all of your help. For anyone looking for resources for nested forms below are some helpful resources.
YouTube Video Instructions API dock for fields_for
Go Rails video for Form Nested Attributes and Fields For in Rails
Active Record Nested Attributes
Rails Guide: Active Record Associations

Rails Simple Form Association submission not updating

I have a problem with has_many relationships setting up forms that do not update the values after submission.
Example
Upon submission of a new character, vn_id does not get updated and in Rails Consoles when I try to check for characters in a Vn, it returns empty.
I am trying to set up a form for characters which belongs to Vn which will be linked through the association but upon submission, it is not linked to Vn.
class Character < ActiveRecord::Base
validates :name, :presence => true
validates :summary, :presence => true
belongs_to :vn
end
class Vn < ActiveRecord::Base
has_many :characters
validates :name, presence: true
accepts_nested_attributes_for :characters
end
Form to create a new Character
<%= simple_form_for #character do |f| %>
<div class="col-lg-12">
<%= f.input :summary,input_html: {style: "height:150px;"} %>
</div>
<div class="col-lg-12">
<%= f.association :vn, as: :check_boxes %>
</div>
<%= f.button :submit , class: "btn btn-primary" %>
<% end %>
Controllers
class CharactersController < ApplicationController
def show
#character = Character.find(params[:id])
end
def new
#character = Character.new
end
def create
#character = Character.new(char_params)
if #character.save
else
render :action=>"new"
end
end
private
def char_params
params.require(:character).permit(:name, :summary,:voiceactor,:vn_name,vn_id: [])
end
end
class VnsController < ApplicationController
def show
#vn = Vn.find(params[:id])
end
def new
#vn = Vn.new
end
def create
#vn = Vn.new(vn_params)
if #vn.save
else
render :action=>"new"
end
end
private
def vn_params
def vn_params
params.require(:vn).permit(:name, :summary,:genre,:developer,:rating,vn_id: [])
end
end
end
Submission unpermitted vn_id
Parameters: {"utf8"=>"✓", "authenticity_token"=>"O2s6GVs77GGUMC5u3eZ9ebv/0l5u0MwP44yS8WGCQnjgwSgHfkbCmhEOUo6WKIMSMo5IfDuNYtMzyphnT/5cwQ==", "character"=>{"name"=>"2222", "voiceactor"=>"111", "summary"=>"one two tthee", "vn_id"=>"32"}, "commit"=>"Create Character"}
Unpermitted parameter: vn_id
(0.1ms) begin transaction
SQL (0.6ms) INSERT INTO "characters" ("name", "summary", "voiceactor", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["name", "2222"], ["summary", "one two tthee"], ["voiceactor", "111"], ["created_at", "2015-10-23 10:34:00.285447"], ["updated_at", "2015-10-23 10:34:00.285447"]
The problem is you're trying to permit an array for a singular association.
The f.association input is for your belongs_to association, so why would it allow multiple records?
You can even see how this works here:
The above are methods only for has_many
In short, they don't exist for belongs_to
Thus, when you call f.association :vn, you're populating an attribute vn_id which can then be associated in your database.
The only time you'd have vn_ids is if you used has_many etc.
This means...
def character_params
params.require(:character).permit(:vn_id)
end
with
f.association :vn
... should work
Somehow, changing vn_id: [] in CharactersController back to :vn_id worked.

Rails app creating join records (has_many through) with a form fields_for section and collection_select

I'm tearing my hair out with this. I have three models: Game, Genre and GameGenre. Games have many Genres through GameGenres, Genres have many Games through GameGenres.
I have a games/new form which creates new game records. To this form, I've added a fields_for :game_genres section, which is supposed to allow the user to add game_genre relationship records to the new game records they create.
I think I'm pretty close to getting this working. At the moment, on submission it returns the error: "Game genres game can't be blank." I take this to mean the form is connecting to the game_genres table. I'm not sure why the controller isn't submitting the :game_id value.
Here's my code.
new.html.erb
<%= form_for(#game) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="form-group">
<%= f.label :title_en, "Game title (English)" %>
<%= f.text_field :title_en, class: 'form-control',
placeholder: "E.g. Marble Madness" %>
</div>
<div class="form-group">
<%= f.label :year, "Year" %>
<%= f.select :year, options_for_select((1950..2017).reverse_each),
{},
{:class => 'form-control'} %>
</div>
<div class="form-group">
<%= f.label :description_en, "Summary" %>
<%= f.text_area :description_en, class: 'form-control' %>
</div>
<hr />
<div class="form-group">
<%= f.fields_for :game_genres do |gf| %>
<%= gf.label :genre_id, "Select a genre" %>
<%= gf.collection_select(:genre_id, Genre.all, :id, :name,
{},
{:class => "form-control"}) %>
<% end %>
</div>
<%= f.submit "Save", class: "btn btn-primary center-block" %>
<% end %>
games_controller.rb (excerpt)
def new
#game = current_user.games.build
#game.game_genres.build
end
def create
#game = current_user.games.build(game_params)
if #game.save
flash[:success] = "Game saved: #{#game.title_en} (#{#game.year})"
redirect_to game_path(#game.id)
else
render 'new'
end
end
private
def game_params
params.require(:game).permit(:title_en, :year, :description_en,
game_genres_attributes: [:genre_id])
game.rb
class Game < ActiveRecord::Base
belongs_to :user
has_many :game_genres, foreign_key: :game_id,
dependent: :destroy
has_many :genres, through: :game_genres
accepts_nested_attributes_for :game_genres,
allow_destroy: true
genre.rb
class Genre < ActiveRecord::Base
belongs_to :user
has_many :game_genres, foreign_key: :genre_id,
dependent: :destroy
has_many :games, through: :game_genres
game_genre.rb
class GameGenre < ActiveRecord::Base
belongs_to :game, inverse_of: :game_genres
belongs_to :genre, inverse_of: :game_genres
As I understand it I do not need to add resources for game_genres to routes.rb.
This is the output from the rails development server when I try to submit a new game record:
Started POST "/games" for ::1 at 2015-06-30 18:59:24 +0100
Processing by GamesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"cUbIu3i9niI0R8b9E3KZeYEbNpx8NDj9Jx1MeGvylYDKQj54d3BwSdLsMiPsRBoUTzoTEuMGTk5/KTKAXH7DMA==", "game"=>{"title_en"=>"fdsdcdc", "year"=>"2017", "description_en"=>"dsdc", "game_genres_attributes"=>{"0"=>{"genre_id"=>"8"}}}, "commit"=>"Save"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
(0.1ms) BEGIN
Genre Load (0.3ms) SELECT "genres".* FROM "genres" WHERE "genres"."id" = $1 ORDER BY "genres"."name" ASC LIMIT 1 [["id", 8]]
(0.1ms) ROLLBACK
Rendered shared/_error_messages.html.erb (0.6ms)
Genre Load (0.3ms) SELECT "genres".* FROM "genres" ORDER BY "genres"."name" ASC
As I understand it, the game_genres_attributes are properly nested and permitted. Why isn't game_id being recognised and saved?
Thanks so much to anyone who can help.
EDIT:
I got the form working by removing this validation from the GameGenre model:
validates :game_id, presence: true
I want this line in though, so I'm now wondering how to work around it?
EDIT 2:
I guess this is the answer (from http://makandracards.com/makandra/1346-popular-mistakes-when-using-nested-forms):
Nested records are validating the presence of their nesting parent
record’s foreign key. If you do this, you cannot create a new parent
record together with a new child record and will need to save the
parent before you can save the child. You can opt to only show the
nested child form when the parent has been saved before, or simply let
go of the validation.

Resources