[英]Instance variable not passed into view in Rails
編輯:解決。 解:
包括gem 'rabl'
和gem 'oj'
在你的Gemfile沿着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
有任何想法嗎?
您用於查找記錄的ID與您數據庫中的ID不匹配。
檢查params[:id]
的計算結果,並確保您可以使用該值查找記錄。
找到解決方案后,您的Gemfile中需要以下三行:
gem 'rabl-rails'
gem 'rabl'
gem 'oj'
以前,我只有rabl-rails,並且我認為這是造成問題的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.