Rails rendering template with status

What is the easiest or shortest way to respond in an API controller. Currently, the following work is being done:

respond_to do |format|
  format.json { render 'client', status: :ok }
end

however, this controller will only respond to json (response_to: json), so all response_to do | format | thing seems like unnecessary code.

Ideally, I just wanted to do something simple:

render 'client', status: :ok

Update: I forgot to mention that: "client" is a jbuilder template that does not match the name of my action.

+3
source share
3 answers

You can directly use rendering

render json: 'client', status: :ok
+4
source

According to @hassasin, you can specify your controller in a format render json:for each action of your controller.

- config.routes.rb, , . contacts_controller:

resources :contacts, defaults: {format: :json}

status, :

def index
  render status: :ok # 200, 400, 500, what ever you want
end

Rails 3.2.16

, .

+3

Since you are using views json(suppose you are using jbuilder), you do not need this rendering operator if your action name matches the name of the view.

+1
source

All Articles