小编Jos*_*ree的帖子

accept_nested_attributes_for:allow_destroy,:_ destroy无效

我有一个Rails 4.1应用程序,它使用了一些值得注意的技术.

简单形式,茧

我无法销毁嵌套属性的记录.基于一些冗长的研究,我相信我的代码是正确的,但我可能会遗漏一些愚蠢的东西.

模型

has_many :staff_services_joins, :dependent => :destroy
has_many :services, :through => :staff_services_joins
accepts_nested_attributes_for :staff_services_joins, :allow_destroy => true
Run Code Online (Sandbox Code Playgroud)

这有点不正统,但我在连接模型上有两个组件,它们不是外键,需要设置.这就是为什么我接受连接模型的嵌套属性而不是服务模型.

控制器方法

def update
ap staff_params
# if @staff_member.update_with_account staff_params, params[:password]
if @staff_member.update_attributes staff_params
  flash[:notice] = 'Successfully updated staff member! :)'
  redirect_to vendor_store_staff_path current_store, @staff_member
else
  flash[:error] = 'Failed to update staff member :('
  render :edit
end end
Run Code Online (Sandbox Code Playgroud)

参数很强

params.require(:staff).permit(
    :user_id,
    :store_id,
    :avatar,
    :remote_avatar_url,
    :first_name,
    :last_name,
    :email,
    :active,
    :admin,
    :staff_services_joins_attributes => [
        :staff_id,
        :service_id,
        :price,
        :duration,
        :_destroy
    ]
)
Run Code Online (Sandbox Code Playgroud)

示例更新参数hash …

ruby activerecord ruby-on-rails simple-form ruby-on-rails-4

4
推荐指数
1
解决办法
3344
查看次数