0

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?

thiebo
  • 1,339
  • 1
  • 17
  • 37
  • Possible duplicate of [Rails 4 - How to render JSON regardless of requested format?](https://stackoverflow.com/questions/23946630/rails-4-how-to-render-json-regardless-of-requested-format) – Veridian Dynamics Mar 21 '19 at 18:47

1 Answers1

1

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
Veridian Dynamics
  • 1,405
  • 1
  • 9
  • 19