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 6768317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:03:39+00:00 2026-05-26T15:03:39+00:00

I am working on a backbone.js application with rails api which provides the json

  • 0

I am working on a backbone.js application with rails api which provides the json as shown:

   [{
        "title": "Da vinci Code",
        "price": "50"
    },
    {
        "title": "Some other name",
        "price": "45"
    }],

TODO:

  1. When a user visits the url “http://www.backboneapp.com/authors/1″ , it should display a list of books belonging to the author.

Progress:
– Server side-Rails [authorscontroller#show]

def show
  @author = Author.find(params[:id])
  @books  = @author.books
  respond_to do |format|
    format.json { render :json => @books }
    format.html
  end
end

2.Client-side-Backbone.js

Router:

routes: {
  'authors/:id': 'showauthor'
},

showauthor: function(id) {

  $('#container').empty();
  window.available_books = new AvailableBookList({
    id: id
  });

  this.availablebooklistview = new AvailableBookListView({
    id: id,
    collection:  available_books
  });

  $('#container').append(this.availablebooklistview.render().el);
}

AvailableBookList Collection:

AvailableBookList = Backbone.Collection.extend({
  model: AvailableBook,
  url: "/authors/" + this.id  
})

The View part:

$(document).ready(function(){

AvailableBookListView = Backbone.View.extend({
  tagName:   'section',
  className: 'availablebooklist',

  initialize: function() {
    _.bindAll(this, 'render');
    this.collection.bind('reset', this.render);
  },

  render: function() {
    this.collection.each(function(available_book) {
      var view = new AvailableBookView({
         model: available_book,
      });

      this.$('.availablebooklist').append(view.render().el);
    });
    return this;
  }
});

});

Problem: when i visit “http://www.backboneapp.com/authors/1″ I am getting 404 error.
In console.log it shows:
get: http://www.backboneapp.com/authors/undefined

In Rails log: parameters {id => undefined}

*Where am i going wrong? *

Any help and suggestions are highly appreciated

  • 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-05-26T15:03:40+00:00Added an answer on May 26, 2026 at 3:03 pm

    I’m going to assume that you didn’t mean new AuthorList in your router, but rather new AvailableBookList.

    You have two problems in your collection:

    1. In the definition of your url, this refers to the window object.
    2. The id parameter you’re passing in the constructor is not applied to your collection instance. It works this way for views only (not for models nor collections).

    To solve the first problem, you can use a function to define a collection url, which would put you in the correct scope:

    AvailableBookList = Backbone.Collection.extend({
      model: AvailableBook,
      url: function() {
        return "/authors/" + this.id;
      }
    });
    

    For the second one, you can define an initialize function and set the id attribute in there:

    AvailableBookList = Backbone.Collection.extend({
      initialize: function(models, options) {
        this.id = options.id;
      },
      model: AvailableBook,
      url: function() {
        return "/authors/" + this.id;
      }
    });
    

    For readability purposes, I would also suggest renaming id to author_id:

    AvailableBookList = Backbone.Collection.extend({
      initialize: function(models, options) {
        this.author_id = options.author_id;
      },
      model: AvailableBook,
      url: function() {
        return "/authors/" + this.author_id;
      }
    });
    

    Update

    You’re also initializing your collection in the wrong way. According to the Backbone.js source, the constructor signature is models, options:

    window.available_books = new AvailableBookList(null, {
      id: id
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a backbone.js application with rails api which provides the json
Working with python interactively, it's sometimes necessary to display a result which is some
Currently I'm working on a Server-Client system which will be the backbone of my
Working on some matrix code, I'm concerned of performance issues. here's how it works
Working on some legacy hibernate code. How do I do the following with hbm.xml(hibernate
I'm trying to get my backbone associations working inside a rails app , and
I have some Backbone.js code that bind a click event to a button, and
I'm trying to test some backbone.js views using Jasmine (via jasmine-headless-webkit). Everything is working
If I click really fast on some elements, Backbone.js will stop working. That is...all
We're designing a backbone application, in which each server-side collection has the potential to

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.