0

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

Community
  • 1
  • 1
Terry Raimondo
  • 629
  • 7
  • 25
  • what is the console output for the request? It would be easier for us to help you out if what's being passed over to the server. – Yosep Kim Aug 19 '15 at 18:01
  • You really should not have the permitted_params method in your model. Dealing with user input is the controllers job, not the model. If you really need to dry out the params whitelist use a module or helper method. – max Aug 19 '15 at 18:06
  • Can you please add the controller? – max Aug 19 '15 at 18:51
  • @max I added the controller, why permitted_params should not be in the model ? – Terry Raimondo Aug 20 '15 at 10:39
  • @YosepKim I'm sorry but the output is really heavy, it is a deeply nested form – Terry Raimondo Aug 20 '15 at 10:42

1 Answers1

0

It sounds like you need to use inverse of in your class. Look it up in the rails docs, also this article gives a pretty good explanation.

Btw I agree with @max's comments re permitted parameters belonging in the controller. Below is how you whitelist params in your controller and include attributes for an association.

def user_params
  params.require(:user).permit(
    :id, :last_name, bank_account_attributes: [:id, :account_number, :sort_code]
  )
end
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
margo
  • 2,927
  • 1
  • 14
  • 31
  • Still run into this issue with inverse_of. has_many :projects, inverse_of: :user and belongs_to :user, inverse_of: :projects – Terry Raimondo Aug 20 '15 at 10:51
  • I think :inverse_of would be useful in the case my form is a user form with a project and not a project form with a user. For my user is already existing and not my project, I can't update it in this form with this solution. – Terry Raimondo Aug 21 '15 at 08:32