正在更改ActiveModel对象的respons_to-url



摘要

如何自定义respond_to为ActiveModel对象生成的路径?

更新:我正在寻找一个钩子、方法重写或配置更改来实现这一点,而不是一个变通方法。(解决方法很简单,但并不优雅。(

上下文&示例

这里有一个例子来说明。我有一个模型Contract,它有很多字段:

class Contract < ActiveRecord::Base
  # cumbersome, too much for a UI form
end

为了使UI代码更容易使用,我有一个更简单的类SimpleContract:

class SimpleContract
  include ActiveModel::Model
  # ...
  def contract_attributes
    # convert SimpleContract attributes to Contract attributes
  end
  def save
    Contract.new(contract_attributes).save
  end
end

这很好用,但我的控制器有问题。。。

class ContractsController < ApplicationController
  # ...
  def create
    @contract = SimpleContract.new(contract_params)
    flash[:notice] = "Created Contract." if @contract.save
    respond_with(@contract)
  end
  # ...
end

问题是respond_with指向simple_contract_url我希望它指向contract_url。最好的方法是什么?(请注意,我使用的是ActiveModel。(

(注意:我使用的是Rails4Beta,但这不是我问题的核心。我认为Rails3的一个好答案也会起作用。(

边栏:如果这种将模型封装在轻量级ActiveModel类中的方法对你来说是不明智的,请在评论中告诉我。就我个人而言,我喜欢它,因为它使我原来的模型保持简单。"包装器"模型处理一些UI细节,这些细节被有意简化并给出合理的默认值。

首先,这里有一个有效的答案:

class SimpleContract
  include ActiveModel::Model
  def self.model_name
    ActiveModel::Name.new(self, nil, "Contract")
  end
end

我根据kinopyo对更改模型输入名称的回答改编了这个答案。

现在,为什么。respond_to的调用堆栈在某种程度上涉及其中。

# Start with `respond_with` in `ActionController`. Here is part of it:
def respond_with(*resources, &block)
  # ...
  (options.delete(:responder) || self.class.responder).call(self, resources, options)
end
# That takes us to `call` in `ActionController:Responder`:
def self.call(*args)
  new(*args).respond
end
# Now, to `respond` (still in `ActionController:Responder`):
def respond
  method = "to_#{format}"
  respond_to?(method) ? send(method) : to_format
end
# Then to `to_html` (still in `ActionController:Responder`):
def to_html
  default_render
rescue ActionView::MissingTemplate => e
  navigation_behavior(e)
end
# Then to `default_render`:
def default_render
  if @default_response
    @default_response.call(options)
  else
    controller.default_render(options)
  end
end

这是我目前所能做到的。我实际上还没有找到URL的构造位置。我知道它是基于model_name发生的,但我还没有找到发生它的代码行。

我不确定我是否完全理解你的问题,但你能做这样的事情吗?

class SimpleContract
  include ActiveModel::Model
  attr_accessor :contract
  # ...
  def contract_attributes
    # convert SimpleContract attributes to Contract attributes
  end
  def save
    self.contract = Contract.new(contract_attributes)
    contract.save
  end
end

-

class ContractsController < ApplicationController
  # ...
  def create
    @simple_contract = SimpleContract.new(contract_params)
    flash[:notice] = "Created Contract." if @simple_contract.save
    respond_with(@simple_contract.contract)
  end
  # ...
end

我可能离基地太远了。希望这至少能激发你的想法。

最新更新