Using Rails 4.2.3
I want to create a form to update a model (User) and create new ones (Project, Rewards, BankAccount)
My models:
class User < ActiveRecord::Base
has_many :projects
def self.permitted_params
[:id, :last_name, ... , bank_account_attributes: BankAccount.permitted_params]
end
...
end
class Project < ActiveRecord::Base
belongs_to :user
accepts_nested_attributes_for :user
def self.permitted_params
[:name, :description, ...]
end
...
end
Form:
<%= simple_form_for @project, html: { class: 'saveable-form' } do |f| %>
<%= f.input :name %>
...
<%= f.simple_fields_for :user do |user| %>
<%= render 'user_fields', f: user %>
<% end %>
<% end %>
Controller
class ProjectsController < ApplicationController
def new
@project = Project.new
@project.user = current_user
@project.user.build_bank_account
end
...
def project_params
params.require(:project).permit(
Project.permitted_params,
rewards_attributes: Reward.permitted_params,
user_attributes: User.permitted_params
)
end
end
It is a really simple form.
But when I try to submit I got this:
Couldn't find User with ID=1 for Project with ID=
I got a temporary fix for this, found here
This is a fix for Rails 2.3.8. I hope there is a DRYer solution for this.
Suggestions ? Do anyone have already encountered this problem and how do you have fixed this ?
Here is the full code:
Form: http://pastebin.com/NwfxCJQH
Controller: http://pastebin.com/U2g2Sv04
Models: http://pastebin.com/5QsCsXN6