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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:32:49+00:00 2026-05-23T01:32:49+00:00

In a standard Rails controller, I’d create a record like this: @user = User.new(params[:user])

  • 0

In a standard Rails controller, I’d create a record like this:

@user = User.new(params[:user])

This assumes that the form parameters that come in are nested.

I’ve been playing with Backbone.js and I noticed that by default, Backbone doesn’t nest the parameters the way a normal Rails form might, which is actually something I expected. So I’m wondering what I should do…

Do I

figure out on the server-side if it’s a request from Backbone by looking at accepts headers, etc and manipulate the params myself so I can keep my controller code small:

do_some_params_manipulation_with(params)
@user = User.new(params[:user])
respond_to do |format|
  if @user.save
    format.html {redirect_to users_url}
    format.json {render :json => @user.to_json }
  end
end

Or, do I instantiate the object in each branch which ends up with repeated code but might be more maintainable in the long run….

respond_to do |format|
  format.html do
    @user = User.new(params[:user])
    if @user.save
      redirect_to users_url
    end
  end

  format.json do
    @user = User.new(params) # and rely on mass-assignment protection
    if @user.save
      render :json => @user.to_json
    end
  end

end

or do I modify my Backbone.js models by overriding the .toJSON method (which I’m not entirely sure how to do because I don’t know enough about Backbone.js yet) so that it nests the params?

In this situation, I have access to both sides of the app, but I am interested in what others are doing.

  • 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-23T01:32:50+00:00Added an answer on May 23, 2026 at 1:32 am

    It’s nice when you can have the general Rails forms and Backbone forms match with respect to the root node. That’s why in my last application I chose to override the Backbone models’ toJSON method.

    You could override the global toJSON method as Raimonds Simanovskis suggested. But even the non-DRY way approach isn’t so bad. Just one line of boilerplate for each model definition:

    // Depends on Underscore.js
    User = Backbone.Model.extend({
      toJSON: function() {
        return { user: _.clone( this.attributes ) }
      },
      // Your other methods here
    });
    

    Edit: Corrected code sample. Sorry for the errors, I was translating from CoffeeScript to JavaScript.

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

Sidebar

Related Questions

No related questions found

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.