編輯:解決。解決方案:實例變量未傳遞到視圖中Rails
包括在你的Gemfile 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的回答) – chintanparikh