can I use rabl without using .json in the url?

38 Views Asked by At

I have rabl up and running.

I have this in routes:

get 'biblios/collection/:biblio_urn' => 'biblios#biblio_rabl', as: 'collection_biblio'

in the controller:

def biblio_rabl
    biblio = Biblio.where(biblio_urn: params[:biblio_urn]).take     
end

This url points to the correct result :

http://localhost:3000/dts/biblios/collection/urn:cts:froLit:ed_desmarez:1900

I would like that url to always respond using rabl and showing the the template dts/biblios/biblio_rabl.json.rabl

I mean without adding .json at the end of the url.

I have tried this in the routes.rb, but it doesn't redirect :

get 'biblios/collection/:biblio_urn' => 'biblios#biblio_rabl', as: 'collection_biblio', to: redirect('biblios/collection/%{biblio_urn}.json')

Is that possible at all?

1

There are 1 best solutions below

0
Veridian Dynamics On

You can force the response to be json by changing the request format in the controller:

request.format = :json

Then make sure you have a respond_to block like this because it's always better to be explicit about your responses:

def biblio_rabl
  respond_to do |format|
    format.json { json: Biblio.where(biblio_urn: params[:biblio_urn]).take }
  end
end