ruby on rails - Update an existing model (User) in a nested form for a new model (Project) -
using rails 4.2.3
i want create form update model (user) , 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' } |f| %> <%= f.input :name %> ... <%= f.simple_fields_for :user |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 simple form.
but when try submit got this:
couldn't find user id=1 project id=
i got temporary fix this, found here
this fix rails 2.3.8. hope there dryer solution this.
suggestions ? have encountered problem , how have fixed ?
here full code:
form: http://pastebin.com/nwfxcjqh
controller: http://pastebin.com/u2g2sv04
models: http://pastebin.com/5qscsxn6
it sounds need use inverse of in class. in rails docs, this article gives pretty explanation.
btw agree @max's comments re permitted parameters belonging in controller. below how whitelist params in controller , include attributes association.
def user_params params.require(:user).permit( :id, :last_name, bank_account_attributes: [:id, :account_number, :sort_code] ) end
Comments
Post a Comment