如何为嵌套表单的允许参数添加嵌套属性,以使嵌套表单起作用

时间:2014-09-09 00:15:27

标签: ruby-on-rails-4 devise nested-forms railsapps

我正在跑步:

  • rails 4.1.4
  • 设计3.3

我使用RailsApp启动应用程序来设置设计和专家。现在我无法进行嵌套表单属性保存。我收到一个错误:

Unpermitted parameters: players

我编辑了初始化文件以显示:

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

我的模特是:

class Users < ActiveRecord::Base
    has_one :player

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

玩家控制器

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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

我设法通过遵循本指南来获得嵌套属性。

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