简体   繁体   中英

Instance variable not passed into view in Rails

EDIT: Solved. Solution:

Include gem 'rabl' and gem 'oj' in your gemfile along with gem rabl-rails

For some reason, my instance variable isn't being passed into the view (I'm using Rabl).

Here's the relevant code:

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

The error:

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

Any ideas?

The ID you're using to look up the record does not match one within your database.

Check what params[:id] is evaluating to and ensure that you can locate a record using that value.

Found the solution, you need these three lines in your Gemfile:

gem 'rabl-rails'
gem 'rabl'
gem 'oj'

Before, I only had rabl-rails, and I assume that was causing the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM