【问题标题】:Rails - How to add a format to an url helper?Rails - 如何向 url 助手添加格式?
【发布时间】:2013-05-18 01:29:49
【问题描述】:

如何让 URL 助手以给定的请求/响应格式创建 URL?

例如,在我的测试中,我有以下内容:

get(activity_url(activity))

这将返回一个关于@response 对象的HTML。我希望有以下相同的行为:

get({controller: 'activities', action: 'show', id: activity.id, format: 'js'})

但没有所有这些措辞。有可能吗?

【问题讨论】:

    标签: ruby-on-rails request helper url-helper


    【解决方案1】:

    你应该可以做这样的事情:

    some_resource_path(format: :json)
    

    some_resource_url(format: :xml)
    

    【讨论】:

    • 谢谢,它可以作为最后一个参数。你知道我在哪里可以找到这个 url helpers 的文档吗?
    • 缩短语法 'some_resource_path :json' 也有效
    【解决方案2】:

    我对 Rails/Ruby 还很陌生,所以我不知道为什么,但在我的 Rails 3.2 应用程序中,语法必须是:

    some_resource_path(:format => :json)
    

    【讨论】:

    • 您的意思是:format => :json?它与format: :json 相同,但有一些额外的语法糖。但是,format: => :json 似乎是一个语法错误。
    猜你喜欢
    • 2012-08-17
    • 1970-01-01
    • 2014-01-26
    • 2015-02-10
    • 2020-12-20
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多