0

I am trying to continue along the path I was lead down in this post: render show page on submit backbone.js + rails + rails-backbone gem

I am still trying to figure out what I describe in the first paragraph of that question. I was messing around in the firebug console. When I click submit from the root page everything works fine (except the app doesn't navigate to the newly created objects show page). The url changes to http://localhost:3000/#/x (where x is the id of the newly created object). When I click submit again, I get "500 Internal Server Error 22ms". This error comes in the Console > All window with a little red arrow next to it that (when clicked) drops down another section - upon with I click the "HTML" tab - and I get this error:

ActiveModel::MassAssignmentSecurity::Error in UsersController#update

Can't mass-assign protected attributes: created_at, id, updated_at

I think I may have located the issue for this error - I just don't know what to do to fix it. What I think is going on is that I have two different controllers (a home controller, and a users controller) - and both of their index actions define the same thing, here is my users controller index action:

class UsersController < ApplicationController
  # GET /users
  # GET /users.json
  def index
    @users = User.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @users }
    end
  end
  ...
end

Here is my home controller index action:

class HomeController < ApplicationController
  def index
    @users = User.all
  end
end

So as you can see, what I think is going on is that when I click submit the first time (from the home page) @users gets set, and the app gets navigated to the new user's show page (although it doesn't which again - ties this all back to my first question - the url changes, but the content of the page does not, the link for my first question is above).

After the url changes to the show page url (without loading the content), I tried submitting another user form with unique input and I get the mass-assign error above. This seems to be because @users has already been set by the home controller...so that when it is called again from the users controller (upon the second submit) it can't be assigned.

How would I fix this? I need both the home controller and the users controller to be able to use @users...I think. And, tying everything back to to my first question here: render show page on submit backbone.js + rails + rails-backbone gem

Can I do something from within the home controller that redirects the newly created user to the show page (maybe in the create, or update method or users controller/or index method of home controller)? Might this solve the issue I have with redirection to the show page from the root page? All the relevant backbone.js (and my root index.html.erb file) code is posted in my first question...please refer to it as it will fill you in on all the backbone details, and what I am really trying to do.

THANKS!

UPDATE

Here is the update action in my users controller (where the error is supposedly coming from):

def update
  @user = User.find(params[:id])

  respond_to do |format|
    if @user.update_attributes(params[:user])
      format.html { redirect_to @user, notice: 'User was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end
Community
  • 1
  • 1
ewizard
  • 2,801
  • 4
  • 52
  • 110

2 Answers2

0

You are not supposed to send created_at and updated_at attributes to server.

You should inspect parameters you are sending and make sure you are not sending these.

By the way, did my other answer help you?

sites
  • 21,417
  • 17
  • 87
  • 146
  • hey juan...I appreciate your answers! The answer you gave on the last question helped me to understand what I was doing a little better...but it didn't solve the problem I have with not going to the show page when I click submit on the root page...any ideas? – ewizard May 25 '13 at 16:02
  • where would I be sending the created_at and updated_at parameters? they arent accessible attributes in my users model... – ewizard May 25 '13 at 16:03
  • Im going to add my users controller update action to my post...the error is supposedly coming from there. – ewizard May 25 '13 at 16:06
  • where? what file would I be passing the parameters in? I search for "created_at" throughout my whole project...and it only shows up in my schema.rb – ewizard May 25 '13 at 16:09
  • is this the line causing the problems? `if @user.update_attributes(params[:user])` – ewizard May 25 '13 at 16:47
  • juan...u want me to put my project up on bitbucket? or git? so u can see exactly what happens? – ewizard May 25 '13 at 17:12
0

You should not insert or add created_at, id, updated_at these are generated automatically .......

Grey
  • 676
  • 8
  • 23