如何检查或设置视图模板扩展名

时间:2017-02-17 22:21:40

标签: ruby-on-rails jbuilder

我有jbuilder文件:

$('.zoom--actions .zoom-in').on('click', function () {
        var img = $(this).parents('.image-zoom').find('.zoom--img img');
        var width = img.width();
        var newWidth = width + 100;
        img.width(newWidth);
    }
);
$('.zoom--actions .zoom-out').on('click', function () {
        var img = $(this).parents('.image-zoom').find('.zoom--img img');
        var width = img.width();
        var newWidth = width - 100;
        img.width(newWidth);
    }
);

代码

app/views/api/items/show.json.jbuilder

无效 - render :show #or render :show, template: "api/items/show" ,但代码

template is missing

工作正常。

有什么问题?需要检查什么? 要查看哪个文件或要转储的内容?

2 个答案:

答案 0 :(得分:1)

您必须使用respond_to方法告诉Rails您正在回复json请求。

def show
  respond_to do |format|
    format.json
  end
end

答案 1 :(得分:1)

另一种方法是传递HTTP标头:

headers: {
  "Accept": "application/json"
}

它不需要在控制器方法中指定格式。 和代码:

render :show

现在工作正常。

我评论了一些限制HTTP标头请求的代码。 @Iceman的答案转发我正确的方向检查,所以他的答案标记为answer

相关问题