【发布时间】:2014-08-28 12:09:14
【问题描述】:
我有一个继承自 BaseSwitchesController 的 SwitchesController,用于 json。
module Api
module V1
class SwitchesController < BaseSwitchesController
respond_to :json
end
end
end
我有另一个 SwitchesController,也继承自 BaseSwitchesController,用于 html。
class SwitchesController < BaseSwitchesController
layout 'application'
end
BaseSwitchesController 继承自 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
我想将 json 移动到 API 模块中的 SwitchesController。 问题是 SwitchesController 中的方法正在寻找 views/base_switches 中的视图,但该文件夹不存在。它应该在视图/开关中寻找视图,并选择例如index.json.rabl.
我该如何解决这个问题?谢谢!
【问题讨论】:
-
您是否在 BaseSwitchesController 中硬编码了渲染或重定向路径?向我们展示您的 BaseSwitchesController
-
我对问题做了一些修改。
-
我希望 Rails 在views/base_switches 和中查找views/switches(但不是按这个顺序)。这就是模板继承的工作原理。
标签: ruby-on-rails ruby inheritance ruby-on-rails-4 model-view-controller