I have a SwitchesController that inherits from BaseSwitchesController, for the json.
module Api
module V1
class SwitchesController < BaseSwitchesController
respond_to :json
end
end
end
I have another SwitchesController, also inheriting from BaseSwitchesController, for the html.
class SwitchesController < BaseSwitchesController
layout 'application'
end
BaseSwitchesController inherits from ApplicationController.
class BaseSwitchesController < ApplicationController
def update
respond_to do |format|
if @switch.update(switch_params)
format.html { redirect_to @switch, notice: 'Switch was successfully updated.' }
format.json { render json: @switch.as_json.merge(:message => 'set_switch_value') }
else
format.html { render action: 'edit' }
format.json { render json: @switch.errors, status: :unprocessable_entity }
end
end
end
I want to move the json to the SwitchesController in the API module. The problem is that the methods in SwitchesController are looking for views in views/base_switches, but that folder doesn't exist. It should be looking for views in views/switches, and choose eg index.json.rabl.
How can I fix this? Thanks!
I wouldn't personally mix together API controllers with HTML controllers.
I find two scenarios to be standard.
You do not have a traditional API (with it's own restful routes, versioning etc.) and you respond in both html and json. In which case you access the formats with a .format
extension on the url or by using request headers. This uses the same views for both formats
You do have a separated API in which case you would not mix the views together and it allows you to use ActiveRecordSerializers, Rabl or whatever templating method you want to use.
Also your example goes against subclassing. Subclasses should be specializations of your superclass and should contain what differentiates them from other subclasses. Your superclass contains specialized behaviour from both subclasses. Since I'm assuming you don't want to respond to json outside of your API controller. Hope this helps out
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.