Getting the reason why this fails?

Hi there.

Have this code, and i need to find put why it fails.

Controller


def update
@user = User.find(@params[:id])
if @user.update_attributes(@params[:user])
flash[:notice] = ‘The user was successfully updated.’
redirect_to :action => ‘show’, :id => @user
else
flash[:notice] = “Error: The user have not been updated.”
redirect_to :action => ‘show’
end
end

I have an array, that seem to be fine, and contains the correct
information, how do i get the mysql error, that triggers the
@user.update_attributes to not be true. ??

  • Emil

Are you receiving any specific message in the browser? If you are in
development mode, you should see the error. You can also check your
logs to see if there is more information there.

If you find any messages and are still unsure, please post them here.

Steve

I have checked my log, i have my Terminal running the script/server, and
it responds the following:


Processing UserController#update (for 127.0.0.1 at 2007-01-29 23:53:34)
[POST]
Session ID: b929e530ded93d3e6bfc66830dfd51a2
Parameters: {“user”=>{“news_edit”=>“y”, “name”=>“Emil T. Kampp”,
“news_delete”=>“y”, “page_create”=>“y”, “zip”=>“2700”, “country”=>“EN”,
“user_all_edit”=>“y”, “page_edit”=>“y”, “news_create”=>“y”,
“login”=>“Emil”, “email”=>“[email protected]”},
“commit”=>“Update”, “action”=>“update”, “change”=>{“u”=>“1”}, “id”=>“1”,
“controller”=>“user”}
User Load (0.002301) SELECT * FROM users WHERE (users.id = ‘1’)
LIMIT 1
SQL (0.000190) BEGIN
SQL (0.000164) COMMIT
Rendering within layouts/standard
Rendering user/update
Pages Load (0.001533) SELECT * FROM pages WHERE (in_menu = ‘true’)
ORDER BY place_in_menu asc
Pages Columns (0.002876) SHOW FIELDS FROM pages
Pages Load (0.001068) SELECT * FROM pages WHERE (controller =
‘user’) AND (action = ‘update’) ORDER BY place_in_menu asc LIMIT 1
Completed in 0.02136 (46 reqs/sec) | Rendering: 0.00951 (44%) | DB:
0.01188 (55%) | 200 OK [http://localhost/user/update/1]
User Columns (0.003740) SHOW FIELDS FROM users

But this dosnt help me much, as i dont get the specific mysql error from
this.

As i can se, tha thing that fails, is the @user.update_attributes which
should execute somkind of sql command, to update the table. But i cant
find this mysql string, that ir runs, or the mysql error it should
gennerate.

  • Emil

It looks to me like it may be failing on validations rather than SQL
as there is no update in the log.

Do you have any validations on your model? Are you also showing any
errors in your update view?:
<%= error_messages_for(@user) %>

update_attributes does not directly run an SQL command. Any save or
create command goes through a process before it actually executes SQL;
most notably validations are checked and the call will return false
if any fail.

Steve

Stephen B. wrote:

Do you have any validations on your model? Are you also showing any
errors in your update view?:
<%= error_messages_for(@user) %>

Steve

YES ! =)

This was just what i needed. I have now solved the puzzle, i needed to
get some kind of error message, so i could locate the problem.

Thanx

Hi Emil,

Emil K. wrote:

Have this code, and i need to find put why it fails.

It’s very difficult to help with nothing more than this. Perhaps you’d
like
to help us help you? Post your code. And maybe the error message(s)
you’re
getting.

Bill