Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Routes - Limiting the available formats for a resource

I have a series of resources that I want only available if accessed via the JS format. Rails' route resources gives me the formats plus the standard HTML. Is there a way to specify that only the JS format routes be created?

like image 745
Eric M. Avatar asked Sep 09 '10 17:09

Eric M.


People also ask

What are Rails resource routes?

Any object that you want users to be able to access via URI and perform CRUD (or some subset thereof) operations on can be thought of as a resource. In the Rails sense, it is generally a database table which is represented by a model, and acted on through a controller.

How many types of routes are there in Rails?

Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.

How does routing work in Rails?

Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.

What are the 7 CRUD routes generated by the resources Method resources songs in Rails?

In Rails, there are seven standard CRUD actions: index, show, new, create, edit, update, and destroy, which relate to specific HTTP verbs and are usually implemented using specific ActiveRecord methods.


1 Answers

You must wrap those routes in a scope. Constraints unfortunately don't work as expected in this case.

This is an example of such a block...

scope :format => true, :constraints => { :format => 'json' } do   get '/bar' => "bar#index_with_json" end 

More information can be found here: https://github.com/rails/rails/issues/5548

like image 66
koonse Avatar answered Sep 23 '22 11:09

koonse