【问题标题】:rails helper to populate json feed用于填充 json 提要的 rails 助手
【发布时间】:2015-09-22 12:18:32
【问题描述】:

我想使用辅助方法来指示哪些链接可用于我的 json 提要。我使用像这样的标准 html 进行了设置

  ..........
  if shopping_request.new?
    content += accept_shopping_request(shopping_request)
    content += reject_shopping_request(shopping_request)
  end
  content += view_send_outs_from_me(shopping_request) if shopping_request.send_out_in_process?
  content += view_send_backs_to_me(shopping_request) if shopping_request.send_back_in_process?
  ..........

我正在尝试实现相同的行为,但这次使用的是 jbuilder 文件中的帮助程序。我尝试了各种配置,但无法显示链接。我用一个基本的例子来测试

  def available_routes_as_json(item)
    Jbuilder.new do |json|
      json.contact contact: "mailto:#{item.delivery_contact_email}"
    end
  end

然后是jbuilder文件的相关位

  json.buttons do
    available_routes_as_json(item)
  end

谢谢

【问题讨论】:

    标签: ruby-on-rails json jbuilder


    【解决方案1】:

    答案真的很简单。

    因为 Jbuilder 不是严格意义上的 MVC 模式的一部分,所以我必须在我的控制器中包含帮助程序。

     include ActionController::Helpers
    

    this 几乎总结了正在发生的事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      • 1970-01-01
      • 2011-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多