15

उम्मीद है कि यहां कोई व्यक्ति मुझे सही दिशा में इंगित कर सकता है।रेल अद्यतन_ट्रिबल्स डीबी मानों को अद्यतन करने का प्रयास करते समय झूठी रिटर्न

मेरे पास एक नियंत्रक अद्यतन def "update_attributes" चल रहा है। वर्तमान में यह कोई त्रुटि संदेश के साथ झूठी वापसी करता है। मैं रूबी के लिए बिल्कुल नया हूं, लेकिन कोडिंग नहीं करना, और यह मुझे कुछ अच्छे दिनों के लिए फंस गया है! मैं नीचे निर्दिष्ट मानों के साथ उपयोगकर्ता मॉडल और डीबी अद्यतन करने की कोशिश कर रहा हूं।

def update 
    #get currently logged in user 
    @user = current_user 
    #update user params based on edit form... 
    if @user.update_attributes(params[:user]) 
     redirect_to profile_path, :notice => "Successfully updated profile." 
    else 
     render :action => 'edit' 
    end 
end 

मेरे संपादन डीईएफ़ ....

def edit 
    @user = current_user 
end 

प्रपत्र इस अद्यतन विधि के लिए निम्न भेजता है:

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess 
utf8: ✓ 
_method: put 
authenticity_token: 9T4ihVI0p8j7pZEFxof3Bfahi2a+o3BPmtXJDnfHT4o= 
user: !ruby/hash:ActiveSupport::HashWithIndifferentAccess 
    first_name: Amanda 
    last_name: Ross 
    is_owner: '1' 
    email: [email protected] 
commit: Update 
action: update 
controller: users 
id: '20' 

और पैरामीटर [: उपयोगकर्ता] रिटर्न:

{"first_name"=>"Amanda", "last_name"=>"Ross", "is_owner"=>"1", "email"=>"[email protected]"} 

ये सभी फ़ील्ड attr_accessible में हैं मॉडल, और मैं बिना किसी समस्या के उपयोगकर्ता बना सकता हूं।

यहाँ development.log निर्गम (खेद यह थोड़ा गंदा है) है ....

Started PUT "https://stackoverflow.com/users/20" for 127.0.0.1 at 2012-04-17 10:39:29 +0100 
Processing by UsersController#update as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"9T4ihVI0p8j7pZEFxof3Bfahi2a+o3BPmtXJDnfHT4o=", "user"=> {"first_name"=>"Amanda", "last_name"=>"Ross", "is_owner"=>"1", "email"=>"[email protected]"},  "commit"=>"Update", "id"=>"20"} 
    [1m[36mUser Load (1.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = 20 LIMIT 1[0m 
>>>>>>>>>>>> 
{"first_name"=>"Amanda", "last_name"=>"Ross", "is_owner"=>"1", "email"=>"[email protected]"} 
    [1m[35m (0.0ms)[0m begin transaction 
    [1m[36mUser Exists (0.0ms)[0m [1mSELECT 1 FROM "users" WHERE (LOWER("users"."email") =  LOWER('[email protected]') AND "users"."id" != 20) LIMIT 1[0m 
    [1m[35mUser Exists (0.0ms)[0m SELECT 1 FROM "users" WHERE ("users"."email" = '[email protected]'  AND "users"."id" != 20) LIMIT 1 
    [1m[36m (0.0ms)[0m [1mrollback transaction[0m 
Rendered users/_form.html.erb (7.0ms) 
Rendered users/edit.html.erb within layouts/application (10.0ms) 
Rendered layouts/_includes.html.erb (30.0ms) 

किसी को संभवतः मदद कर सके बाहर बिंदु जहां मैं गलत, कृपया जा रहा हूँ?

अग्रिम धन्यवाद!

+0

क्या आपने 'development.log' की जांच की है? – Alex

+0

त्रुटि संदेश का विवरण अच्छा होगा - यह नहीं पता कि आप कैसे 'झूठी' – Jonathan

+0

को मुख्य पोस्ट में develop.log आउटपुट जोड़ रहे हैं .... (बस रोलबैक प्राप्त करना) – steve

उत्तर

39

#update_attributes और #save दोनों पहले जांच लेंगे कि क्या आपका मॉडल उदाहरण #valid? डेटाबेस पर लिखने से पहले है या नहीं। यदि आपका मॉडल उदाहरण #valid? नहीं है, तो ये विधियां झूठी वापसी करेंगी। यह देखने के लिए कि आपके मॉडल उदाहरण में क्या गड़बड़ है, अपने मॉडल के #errors पर एक नज़र डालें।

Rails.logger.info(@user.errors.messages.inspect) 

या, अपने विधि में एम्बेडेड,

def update 
    @user = current_user 
    if @user.update_attributes(params[:user]) 
    redirect_to profile_path, :notice => "Successfully updated profile." 
    else 
    # print the errors to the development log 
    Rails.logger.info(@user.errors.messages.inspect) 
    render :action => 'edit' 
    end 
end 
+0

यही है! यही वही है जो मुझे चाहिए! त्रुटि लॉग से पता चला कि मुझे एक ऐसा क्षेत्र नहीं मिला था जिसके साथ इन्हें अपडेट किया जाना आवश्यक था। बहुत बहुत धन्यवाद, अब मुझे पता है कि इन त्रुटियों को ठीक से कैसे डिबग करना है! :-) – steve

+1

मुझे अब भी इसी तरह की समस्या का सामना करना पड़ रहा है, मैंने Rails.logger.info (@ user.errors.messages.inspect) को आजमाया और यह मुझे "सत्य" देता है। इसका क्या मतलब है? मुझे एक कॉलम अपडेट करना है और मैंने attr_accessible में कॉलम जोड़ा है। इसे अपडेट करते समय मुझे झूठा रिटर्न मिलता है और यह पूछताछ करता है कि यह निष्पादन भी अजीब है। यहां देखें http://paste.lisp.org/+2S4Y मैं फ़ील्ड अपडेट नहीं कर सकता। समस्या क्या हो सकती है? –

+0

@ ज्योतिरंजन चेक @ @ user.errors.messages.inspect' यह रेलवे 4.2 के साथ कम से कम मेरे मामले में काम करता है – jmarceli

4

तुम भी बेहतर आत्मनिरीक्षण जब बजाय update_attributes! बुला, जो अपवाद को जन्म देती है के बजाय सिर्फ झूठी वापस लौट कर विशेषताओं को अद्यतन करने के मिल सकता है।

संबंधित मुद्दे

 संबंधित मुद्दे