update_attributes isn't saving to the database

余生颓废 提交于 2019-12-14 01:34:58

问题


The correct custom params are being displayed in my debug function after the form is submitted but the default params are displayed when I enter console.

Controller

def update
    current_user.update_attributes(params[:user])
    flash[:success] = "Your settings have been saved!"
    render new_status_update_path
end                                         

Model

attr_accessible :deficit_pct,
              :target_bf_pct,
              :activity_factor

Notes:

  • The closest question I could find to this on SO is a question that changes the attributes of an object that exists through an association.

  • I've tried using the Object.update method although I get an error that says:

    private method `update' called for #

Any ideas?

Thanks!


回答1:


Try the code :-

def update
    if current_user.update_attributes(params[:user])
        flash[:success] = "Your settings have been saved!"
        render new_status_update_path
    else
        p 111111111111
        p  current_user.errors.inspect
    end
end

after check your log for any errors.exist for that active record




回答2:


After playing around with in the console I've found out that even if I change the attributes manually the attributes don't 'stick' after I exit the console.

So I'll enter console, change the users attributes, test them, and they'll be changed. If I exist and re-enter, THEN test them, they'll have reverted back to their default values.

This leads me to believe that the 'after_initialize' method within the user model which sets its default values is running after each save. I though that it would only run after the object had been saved for the first time alone but now I know it run each time it is saved.

after_initialize :default_values 

def default_values
  self.goal = "Cut"
  self.measurement = "US"
  self.bmr_formula = "katch"
  self.fat_factor = 0.655
  self.protein_factor = 1.25 
  self.deficit_pct = 0.10
  self.target_bf_pct = 0.10
  self.activity_factor = 1.3
end

Once I remove all these values and the after_initialize method, it saves permanently.




回答3:


You should make sure that you don't have any validation errors. You can check them using:

active_record_model.errors

In your case, it would be

current_user.errors

Also, you should print the return value from update_attributes to see if it's true or false. If you get false, the save was cancelled. This was most likely caused by validation errors or a callback returning false.




回答4:


Something like:


def update
    if current_user.update_attributes(params[:user])
        flash[:success] = "Your settings have been saved!"
        render new_status_update_path
    else
        some_error_handling_code
    end
end

Would not display success when the save fails. As a general rule, you should check whether a save, or any other back end operation, fails, before reporting success to the end user.



来源:https://stackoverflow.com/questions/14093922/update-attributes-isnt-saving-to-the-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!