How do you add a nested attribute to permitted parameters for Devise in order for nested form to work

本小妞迷上赌 提交于 2019-12-12 02:58:52

问题


I'm running:

  • rails 4.1.4
  • devise 3.3

I used the RailsApp starter app to set up devise and pundit. Right now I am having trouble making a nested form attribute save. I am getting an error:

Unpermitted parameters: players

I edited the initializer file to show:

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :players, :player_attributes => [:position]) }
  devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :role, :players, :player_attributes => [:position]) }
end

My models are:

class Users < ActiveRecord::Base
    has_one :player

    accepts_nested_attributes_for :player
end
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class Players < ActiveRecord::Base
    belongs_to :user
end

Players Controller

def update
  @player = Player.find[params[:id])
  if @player.update_attributes(secure_params)
     redirect_to players_path, :notice => "Player updated"
  else
     redirect_to players_path, :alert => "Unable to update"
  end
end

private

def secure_params
  params.require(:user).permit(:user_id, :players)
end

What am I doing wrong?


回答1:


I managed to get nested attributes working by following this guide..

http://kakimotonline.com/2014/03/30/extending-devise-registrations-controller/



来源:https://stackoverflow.com/questions/25734944/how-do-you-add-a-nested-attribute-to-permitted-parameters-for-devise-in-order-fo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!