【发布时间】:2014-01-29 00:51:38
【问题描述】:
编辑:已解决。解决方案:
包括
gem 'rabl' 和 gem 'oj' 以及 gem rabl-rails
由于某种原因,我的实例变量没有被传递到视图中(我正在使用 Rabl)。
以下是相关代码:
articles_controller.rb
class ArticlesController < ApplicationController
respond_to :json, :xml
def index
@articles = Article.original.last(100)
end
def after
@articles = Article.where("id > #{params[:id]}")
end
def show
@article = Article.find(params[:id])
end
end
show.json.rabl
object @article
attributes :id, :headline, :source, :link
attributes :similar_articles => :similar
错误:
RuntimeError in Articles#show
Showing /Users/chintanparikh/Dropbox/Projects/Current/article_aggregator/app/views/articles/show.json.rabl where line #2 raised:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
有什么想法吗?
【问题讨论】:
-
你的 Rails 版本是什么? Rails 4 使用强参数,需要您在控制器中编写参数方法。
-
3.2.13。看起来参数不是问题(阅读我对 Johns 答案的评论)
标签: ruby-on-rails ruby json rabl