嵌套属性 - 不允许的参数 Rails 4 [英] Nested attributes - Unpermitted parameters Rails 4

查看:46
本文介绍了嵌套属性 - 不允许的参数 Rails 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在 Rails 4 中关于强参数的嵌套属性的问题,有很多关于 SO 的资源,但我没有找到任何解决方案:(如果它是重复的,很抱歉)

There is a lot of resources on SO about issues on Nested attributes in Rails 4 regarding strong parameters but I don't find any solution on this: (so sorry if it's a duplicate)

我在会员和个人资料之间建立了 1-1 关系.尝试使用个人资料属性更新成员时,出现此错误:

I have a 1-1 relation between member and profile. When trying to update a member with profile attributes I've got this error:

Unpermitted parameters: profile

我的参数在哪里

===> params: {"member"=>{"profile"=>{"first_name"=>"test", "last_name"=>"test"}, "email"=>"test@test.com"}}

我的模型:

class Member < ActiveRecord::Base
  ...
  has_one :profile
  accepts_nested_attributes_for :profile
end

个人资料.rb

class Profile < ActiveRecord::Base
  belongs_to :member
end

我的表格:

= simple_form_for [:admin, @member] do |f|
  = f.simple_fields_for @member.profile do |pf|
    = pf.input :first_name
    = pf.input :last_name
  = f.input :email
  = f.button :submit

和我的控制器:

class Admin::MembersController < Admin::BaseController
  before_action :set_member, only: [:edit]

  def edit
  end

  def update
    if @member.update(member_params)
      Rails.logger.debug "===> (1)"
      redirect_to edit_admin_member_path
    else
      render action: 'edit'
    end
  end

  private
    def set_member
      @member = Member.find(params[:id])
    end

    def member_params
      params[:member].permit(:email, profile_attributes: [:first_name, :last_name ])
    end
end

我尝试了很多东西,但不明白我的错误在哪里..此外,在更新方法中,它说@member 已正确更新(显示 ===> (1))

I've tried many things but don't understand where is my mistake.. Moreover in the update method it says the @member is correctly updated (shown ===> (1))

推荐答案

好的,明白了..

我认为这是由 simple_form 引起的:

I think this is caused by simple_form:

= simple_form_for [:admin, @member] do |f|
  = f.simple_fields_for :profile, @member.profile do |pf|
    = pf.input :first_name
    = pf.input :last_name
  = f.input :email
  = f.button :submit

这篇关于嵌套属性 - 不允许的参数 Rails 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆