iamtoc
iamtoc

Reputation: 1709

Ruby Rails Only some parameters are passed to the controller

I have a single form of user attributes with about 60 fields, displayed separately by means of toggling hidden divs all on one page. When updating someone else' profile, all fields update as expected. When updating the current logged in profile (current_user), only about 40 of the fields update. Here's what I am observing on the update method for the current_user profile:

When setting a breakpoint directly after @user = User.find(params[:id]) and looking at the parameters that got passed, only about 40 out of the 60 form field parameters are even present. The ones that are present update as expected, and obviously the ones that aren't present don't update.

Any clues as to what might be causing this strange behavior?

Example: one of many mis-behaving form fields on users/_form.erb

<%= f.text_field :street_address, :placeholder => 'address..'  %>

Update Method in users_controller.rb

# UPDATE
  def update   
    @user = User.find(params[:id])
    breakpoint_set = on_this_line
    respond_to do |format|     
      if @user.update_attributes params[:user]
        format.html do
          redirect_to("/users", :notice => 'User Profile was successfully updated.')
          format.xml  { head :ok }
        end
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end

Upvotes: 0

Views: 216

Answers (2)

sameera207
sameera207

Reputation: 16619

Check if the parameters are porting to the controller from client side. You may check this will Firefox + Firebug console.

And check if every parameter is under the user hash, because the missing parameters might not coming in the same user hash (which the Rails controller is looking at).

Upvotes: 1

Michael Slade
Michael Slade

Reputation: 13877

If the "currently logged in user" is just a User object, then I suspect you are seeing the side effects of caching.

If you have two Active Record objects that represent the same record, and they disagree about what the state of that record should be, different kinds of problems can happen. For example;

@u1 = User.find logged_in_user_id
@u2 = User.find logged_in_user_id

@u1.update_attributes :username => "root"

@u2.username   # old username
@u2.reload
@u2.username   # "root"

Make sure this isn't happening to you.

Upvotes: 2

Related Questions