Rails - How to add a format to an url helper?

19.1k Views Asked by At

how can I make an URL helper create an URL in a given request/response format?

For example, in my test I have the following:

get(activity_url(activity))

This will returns me an HTML on the @response object. I'm looking to have the same behavior of the following:

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

but without all this wording. Is it possible?

2

There are 2 best solutions below

2
On BEST ANSWER

You should be able to do something like this:

some_resource_path(format: :json)

or

some_resource_url(format: :xml)
1
On

I'm fairly new to Rails/Ruby, so I'm not sure why, but in my Rails 3.2 application the syntax must be eg:

some_resource_path(:format => :json)