ruby-on-rails – 阻止Rails尝试提供模板/ ActionView :: Missin
发布时间:2020-12-16 23:19:38 所属栏目:百科 来源:网络整理
导读:我有一个简单的角度轨道应用程序,我试图连线. 这是我的rails控制器: class ItemsController ApplicationController respond_to :json,:html def index @items = Item.order(params[:sort]).page(params[:page]).per(15) end def show @item = Item.where(pa
我有一个简单的角度轨道应用程序,我试图连线.
这是我的rails控制器: class ItemsController < ApplicationController respond_to :json,:html def index @items = Item.order(params[:sort]).page(params[:page]).per(15) end def show @item = Item.where(params[:id]) if @item.empty? flash[:alert] = "Item number #{params[:id]} does not exist" else respond_with @item do |format| format.json { render :layout => false } end end end end 我一直收到ActionView :: MissingTemplate错误,因为rails一直试图提供erb模板.我不想要模板!!我只想要json.有人可以给出明确的respond_to / respond_with语法,这将永远摆脱我的模板吗? 解决方法
rails中有两种渲染方式,CMIIW
首先,默认情况下,它将呈现视图模板,例如 def show 然后它将像往常一样渲染节目视图, 然后下一个方法是使用渲染json:…,例子 class ItemsController < ApplicationController respond_to :json,:html def show @item = Item.where(params[:id]) if @item.empty? render json: { message: "Item number #{params[:id]} does not exist",status: :not_found } else render json: @item.to_json end end end rails guide about render非常有用,你可以读它here (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |