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

  • Home
  • SEARCH
  • 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 8813717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:55:45+00:00 2026-06-14T03:55:45+00:00

This is a Noob question regarding Backbone.JS and ActiveRecord. I’d be grateful for a

  • 0

This is a Noob question regarding Backbone.JS and ActiveRecord.
I’d be grateful for a pointer on how to debug this.

I’m trying to use Backbone.Js code to create a “Trainer” object, which has a single attribute, “name” (a string).
The front end of the application is Ruby on Rails. There is a data migration for the Trainer table.

In trainers controller:

def create
  document = Trainer.create! params[:trainer]
  render :json => document
end

Now, in app/assets/javascripts/backbone/views/trainers/new_view.js:

Gym.Views.Trainers.NewView = Backbone.View.extend({
  el : 'div.trainer_form',
  template: JST['backbone/templates/trainers/new_template'],
  model : new window.Gym.Models.Trainer({}),
  initialize: function() {
  this.document = this.options.user;
  Backbone.Validation.bind(this, {
    invalid: function(view, attr, error) {
      $("form#new-trainer .errors ul").append("<li>" + error + "</li>")
    }
  });
  this.render();
},
render : function() {

  $(this.el).html(this.template({trainer:this.model.toJSON()}));
  return this
},
events : {
  "click input#submit_button" : 'create_trainer'
},


create_trainer : function(event) {
  event.preventDefault()
  params = $("form#new-trainer").formParams()
  params['user_id'] = Gym.currentUser.userId
  this.model.save(params, {success : function(model, response) {
    Gym.trainers.add(model)
    Objects.views.selectTrainer.render()

    Gym.current_trainer = model

    $("select#trainer_selector").val(Gym.current_trainer.get('id'))

    Objects.views.new_trainer.model = new Gym.Models.Trainer()
    Objects.views.new_trainer.render()

  }
  });

Now, I can see in the Rails log that I’m getting to the controller:

Started POST "/trainers" ...
Processing by TrainersController#create as JSON
Parameters: {"name"=>"Lori Stevens", "user_id"=>1, "trainer"=>{}}

However, when it gets to the SQL, I see this:

[1m^[[36mSQL (0.4ms)^[[0m  ^[[1mINSERT INTO `trainers` (`created_at`, `name`, `updated_at`, `user_id`) VALUES ('2012-11-07 20:33:09', NULL, '2012-11-07 20:33:09', NULL)^[[0m

The parameter ‘name’ – which comes from the template, and is the attribute of the Trainer object- is not getting to the database, even though the parameter “name” is set in the JSON.

I’d appreciate a pointer on how to debug this – clearly I am not understanding how Backbone.js and ActiveRecord are connected.

  • 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-14T03:55:46+00:00Added an answer on June 14, 2026 at 3:55 am

    The Controller takes the request from your browser and puts the data into ActiveRecord:

    def create
      document = Trainer.create! params[:trainer]
      ...
    end
    

    But then you see request in the log, params[:trainer] equals the empty hash {}

    You can either change the javascript that it creates json with a hash like

    { 'trainer': {'name' : 'Lori stevens', ... }}
    

    I don’t know how easy this in backbone.

    Or you can change your controller that it gets the values out of the hash and constructs an new hash for your trainer model, like it is:

    gotten_name_from_json = params['name']
    ...
    document = Trainer.create!(
       {:name => gotten_name_from_json, :town => gotten_twon_from_json})
    

    I made this verbose to show that using this you can translate what ever json comes in, even when it comes from third parties where you can not control the format.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a noob question. I am trying to use node.js as a JavaScript
This is a noob question. I'm trying to learn how to use SWIG to
Forgive the noob question, but I'm trying to get my head wrapped around this.
another noob question regarding F#. If I have the following code... let ExeC =
I'm new to c# so apologies if this is a noob question. I'm trying
Sorry for this noob question. I'm beginning to use the Unity 2.0 container with
javascript noob here. So I'm trying to use this s3 uploader jQuery example here
Sorry for this noob question, but I'm not sure what I just did regarding
This is a noob question from someone who hasn't written a parser/lexer ever before.
This is a noob question, but I can't get Valum's Uploader to work no

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.