【问题标题】:Rails 4 Strong Parameters with delegateRails 4 带委托的强参数
【发布时间】:2015-05-31 07:18:00
【问题描述】:

我正在尝试接收来自 Celeb 模型的参数并将正确的参数委托给用户:

class Celeb < ActiveRecord::Base

  has_one :user, as: :role, dependent: :destroy

  delegate :name, :username, :email, :password, :password_confirmation, :name=, :username=, :email=, :password=, :password_confirmation=, to: :user
end

在控制器中:

def create
    @celeb = mount_celeb()
    @celeb.assign_attributes celeb_params
    ...

  end

def celeb_params
    params.require(:celeb).permit(:email, :username, :name, :password, :password_confirmation)
end

def mount_celeb
    celeb = Celeb.new
    celeb.build_user
    celeb
  end

但是关联的用户仍然是空的,没有分配任何字段。有没有办法使用强参数来实现这一点?

【问题讨论】:

  • 我看不出它为什么会失败,你确定你在控制器中收到了正确的参数吗?
  • 这是我的错,我正在用 byebug 进行调试,但它失败了,但在实际请求中它有效,只需要刷新即可解决问题。

标签: ruby-on-rails ruby-on-rails-4 activerecord strong-parameters


【解决方案1】:

我的问题中的代码按预期工作。问题是调试模式( byebug )不知何故没有反映我最后的更改。

我这样做的原因是因为我正在创建一个 API,因此最好从文档中抽象嵌套属性(要求客户端在每个请求中发送“user_attributes”会很糟糕)

【讨论】:

    猜你喜欢
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 2014-05-24
    相关资源
    最近更新 更多