Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8491655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:29:24+00:00 2026-06-10T22:29:24+00:00

I created a practice rails app where I have created a namespace and versioned

  • 0

I created a practice rails app where I have created a namespace and versioned like is demonstrated in this railscast. Everything is working fine and I can see the json output in the browser

Then I added the Rabl gem and was trying to render the rabl views but I get an empty JSON array rendered in the browser

Here is what I did systematically to get the versioning working

1) changed the routes file to look like this

App0828::Application.routes.draw do

 namespace :api, defaults: { format: 'json'} do
   namespace :v1 do
    resources :vendors
   end
 end

  #resources :vendors
  #root to: 'vendors#index'
end

2) The created thise files
    app/copntrollers/api/v1/vendors_controller.rb

  Inside the vendors_controller.rb I added the following code

 module Api
  module V1
  class VendorsController < ApplicationController

  class Vendor < ::Vendor
    #subclass vendor class so thatyou can extend its behaviour just for this version
    #add any functions specific to this version here
    end

  respond_to :json

  def index
    respond_with Vendor.all       
  end

  def show
    respond_with Vendor.find(params[:id])
  end
  ..........

3) Then I pointed my browser to this url "http://localhost:3000/api/v1/vendors"
   And I can see the json output in my browser

4) Then I added the rabl gem
5) restarted the server
6) Changed the above file at    app/copntrollers/api/v1/vendors_controller.rb
   from the version above to the version below

  module Api
   module V1
   class VendorsController < ApplicationController

    class Vendor < ::Vendor
      #subclass vendor class so thatyou can extend its behaviour just for this version
      #add any functions specific to this version here
    end

    respond_to :json

  def index
    render 'api/v1/index.json.rabl'
   end

  def show
    render 'api/v1/show.json.rabl'
  end

  .......
 7) I created the following files with this code:
     file: app/views/api/v1/show.json.rabl
     code:    object @vendor
              attributes :id, :name

     file: app/views/api/v1/index.json.rabl
     code:   object @vendors
             attributes :id, :name
 8) Routes file looks like this

 api_v1_vendors GET    /api/v1/vendors(.:format)   api/v1/vendors#index {:format=>"json"}

 POST   /api/v1/vendors(.:format)          api/v1/vendors#create {:format=>"json"}

 new_api_v1_vendor GET    /api/v1/vendors/new(.:format)      api/v1/vendors#new {:format=>"json"}

 edit_api_v1_vendor GET    /api/v1/vendors/:id/edit(.:format) api/v1/vendors#edit {:format=>"json"}

 api_v1_vendor GET    /api/v1/vendors/:id(.:format)      api/v1/vendors#show {:format=>"json"}
 PUT    /api/v1/vendors/:id(.:format)      api/v1/vendors#update {:format=>"json"}

 DELETE /api/v1/vendors/:id(.:format)      api/v1/vendors#destroy {:format=>"json"}

 9) Finally I went to url: "http://localhost:3000/api/v1/vendors.json"

  And all I see in the browser is an empty JSON hash: "{}"

So clearly it cannot access the instance variables. Seems to be some issue with being out of scope. Im not sure how to proceed next. I couldn’t find any examples online of versioning with rabl. Any suggestions? Id really appreciate it

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:29:26+00:00Added an answer on June 10, 2026 at 10:29 pm

    Where are you setting the instance variables?

    First, you probably should have this line: respond_to :json in your Api::ApplicationController (or something like that, basically the controller from which every other controller in the Api module inherit).

    You dont need these: render 'api/v1/index.json.rabl'.

    Just set your instance variables:

    def index
        @vendors = Vendor.all
        respond_with @vendors
    end
    
    def show
        @vendor = Vendor.find(params[:id])
        respond_with @vendor
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails app I am working on that allows users to create
I am working within a Ruby on Rails app. I have a loop that
I have a simple rails app that stores images created by users in an
I have a very standard CRUD app in Rails & Jquery Mobile with two
This is for a Ruby on Rails 3.0.x project. I have a lookup table
I have a rails app. Homepage searches the movies database and displays the results
I have a Rails 2.3 app which tracks an organisation's members, and whether they
I have created a rails application with a specific style, some contained outside the
I'm building this little app with ruby on rails. In the app, I'm having
I'd like to set up a staging server for a Rails app. I use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.