【问题标题】:Passing argument to decorator from controller in Rails using Draper使用 Draper 将参数从 Rails 中的控制器传递给装饰器
【发布时间】:2012-09-27 12:31:18
【问题描述】:

我不知道如何将参数从控制器传递给装饰器:

装饰者:

def as_json(options = nil)
  {
   :name => user.name,
   :dob => user.dob
   :created_at => user.created_at,
   :url => user
  }
end

控制器:

format.json { render :json => UserJsonDecorator.new(@user)}

只向新方法传递一个额外的参数是行不通的:

UserJsonDecorator.new(@user,options)

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails-3 draper


    【解决方案1】:

    我基本上是用错了。

    传递附加参数的正确形式是:

    UserJsonDecorator.new(@user).to_json(options)
    

    【讨论】:

      猜你喜欢
      • 2021-11-21
      • 2014-11-17
      • 2020-05-09
      • 2018-08-24
      • 2014-10-01
      • 1970-01-01
      • 2018-12-04
      • 1970-01-01
      相关资源
      最近更新 更多