开发者

How to debug a Rails3 model that won't update or create?

开发者 https://www.devze.com 2023-03-01 07:55 出处:网络
Help, I\'ve broken my Rails3 user model! I first noticed that I could not update users from the app itself.

Help, I've broken my Rails3 user model!

I first noticed that I could not update users from the app itself.

It appears that I also can't from the rails console.

User.save returns false.

User.create produces a record with id=nil, which is not saved to the database.

I've spent two days trying to work out what the problem is. I've taken out all my CanCan and Devise validations. I've been through the code line by line. I am stumped!

Nothing in the logs is pointing me in an obvious direction.

I commented everything out of User.rb, but no joy.

I know this is a really vague question, that's almost impossible to answer from a far.

What I'm hoping is that someone can suggest logical steps for working through this and trying to figure out what's happening.

I'm still fairly new to Rails, and keen to learn how it all works.

Thanks for your ideas!

UPDATE:

I've been digging into this for the past week. Eventually I started a new rails app from scratch and systematically moved the old app into the new app piece by piece to try and work out where the problem lies.

I've tracked it down to my custom Devise routes.

routes.rb

devise_for :users,  :path_prefix => 'registration', :controllers => {:registrations => 'users/registrations'}

If i remove the :controllers line, then I can update the users name using the Devise user edit form.

However this is not a workable solution. The Devise edit form only provides name and email fields, but there are several other columns in my user model that the user needs access to . Hence the custom routing.

So I've tracked the issue this far, but I'm not sure where to look yet. I tried editing the registrations_controller to the default values, but no luck. (I even cut as pasted the code directly from the devise git repository).

def edit
  super
end

def update
  super   
end

So I don't think the problem is the controller.

Which leaves the form itself. The form is defined as

开发者_Go百科<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put, :multipart => true }) do |f| %>

I can't see any issue with this. And I can't think where else to look to debug this.

To recap, the edit form displays as expected, user can edit information, click update, is redirected as expected, no errors are displayed, but the edited info is not saved. The tail shows no SQL save being called.

Very stumped, and I'd appreciate any wild ideas or suggestions!

Many thanks


Try this to debug in the console:

require 'pp'
u = User.new
u.save
pp u.errors

This should give you the errors that prevent the user from being saved to the DB.


I've eventually tracked this down to a stupid oversight on my part.

In the controller I had

resource.update_attributes!(params[:resource_name])

when what I needed was

resource.update_attributes!(params[resource_name])

An edit I must have made at some point.

A self inflicted issue that took two weeks of head scratching to resolve!

0

精彩评论

暂无评论...
验证码 换一张
取 消