0

I have this controller:

class AccountsController < ApplicationController
  def create
    if @current_account.update_attributes(account_params)
      redirect_to :dashboard
    end
  end

  def account_params
    params.require(:account).permit(:company, users_attributes: [:name, phone_attributes: [:number]])
  end
end

as you can see, i have 2 levels of nested attributes, the issue is I used to add a virtual attribute before using strong parameters like this:

def create
  @phone = @current_account.users.phone.requires_us_format = true
  ...
end

How to achieve this using both nested attributes and strong parameters?

CodeOverload
  • 47,274
  • 54
  • 131
  • 219
  • and simply add `phone_attributes: [:number, :requires_us_format]` doesn't work for you? AFAIK `strong_parameters` only slice and dice your params and doesn't really refer back to how you define your model in anyway. https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/strong_parameters.rb – j03w Aug 13 '13 at 10:53
  • @j03w but how to add `:require_us_format` to the params manually, it doesn't work for me as it returns `undefined method `[]' for nil:NilClass` when i do `params[:account][:users_attributes][0][:phone_attributes][:require_us_format] = true` – CodeOverload Aug 13 '13 at 11:05
  • Try to set requires_us_format on you before_save function on Phone model. – user2801 Aug 13 '13 at 12:07
  • I only want to add the attribute on certain controllers – CodeOverload Aug 13 '13 at 12:11
  • `params[:account][:users_attributes][0][:phone_attributes][0][:require_us_format]`? but from your error `params[:account][:users_attributes][0][:phone_attributes]` is `nil` though so make sure you actually have `phone_attributes` created on your params – j03w Aug 13 '13 at 12:29

0 Answers0