2015-12-10 14 views
11

मैंने इस पोस्ट को व्यवस्थित करने का प्रयास किया है, इसलिए यह नए comers के लिए इतना लंबा प्रतीत नहीं होता है। इसमें नीचे दिए गए उत्तरों में दूसरों से निम्नलिखित सुझावों के परिणामस्वरूप त्रुटियां शामिल हैं।रेल 4 - सेटअप, स्वचालित रूप से प्रोफ़ाइल बनाते हैं जब नया उपयोगकर्ता

मैं रेल 4.

मैं वसीयत और omniauth का उपयोग के साथ किसी ऐप्लिकेशन बनाने की कोशिश कर रहा हूँ।

मेरे पास उपयोगकर्ता और प्रोफाइल के लिए मॉडल हैं। उपयोगकर्ता के पास एक प्रोफ़ाइल है और प्रोफ़ाइल उपयोगकर्ता से संबंधित है।

मेरा उद्देश्य उन सभी चीजों के लिए उपयोगकर्ता मॉडल का उपयोग करना है जो उपयोगकर्ता वास्तव में स्पर्श नहीं करते हैं। मैं उन चीज़ों के लिए प्रोफाइल मॉडल का उपयोग करता हूं जो उपयोगकर्ता बनाए रखता है।

जब कोई नया उपयोगकर्ता बनाया जाता है, तो मैं चाहता हूं कि उपयोगकर्ता को उनके प्रोफ़ाइल पेज पर रीडायरेक्ट किया जाए, जिसे उपयोगकर्ता मॉडल में बनाए गए विवरणों के साथ आंशिक रूप से आबादी दी जानी चाहिए।

मुझे परेशानी हो रही है।

मेरे पास है:

User.rb

has_one :profile 

Profile.rb

belongs_to :user 

Omniauth_callbacks_controller.rb

if @user.persisted? 
    sign_in_and_redirect_user(:user, authentication.user.profile) 
    set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format? 
else 
    session["devise.#{provider}_data"] = env["omniauth.auth"] 
    redirect_to new_user_registration_url 
end 

मैं भी कॉल में की कोशिश की है वापस अनुप्रेषित :

sign_in_and_redirect @user, event: :authentication 

प्रोफाइल/show.html.erb

<div class="container-fluid"> 
    <div class="row"> 
     <div class="col-xs 8 col-xs-offset-1"> 
      <%= @profile.user.formal_name %> 


      <%= @profile.occupation %> 
      <%= @profile.qualification.awards %> 
      <%= @profile.overview %> 
      <%= @profile.research_interests %> 

     </div> 

     <div class="col-xs 3"> 
      <div class="geobox"> 
      <%= render 'addresses/location' %> 
      <%= @profile.working_languages %> 
      <%= @profile.external_profile %> 

      </div> 
     </div> 
    </div> 

    <div class="row"> 
     <div class="col-xs 10 col-xs-offset-1"> 
      <%= render 'profiles/activity' %> 
     </div> 
    </div> 

</div> 

_nav.html.erb

<% if user_signed_in? %> 
        Hi <%= link_to(current_user.first_name.titlecase, profile_path(current_user)) %></span> 

मैं भी कोशिश की है:

_nav.html.erb

<% if user_signed_in? %> 
        Hi <%= link_to(current_user.first_name.titlecase, user_profile_path(current_user.profile)) %></span> 

कोई नया उपयोगकर्ता अपने प्रोफाइल पेज पर जाने के लिए कुछ प्रकार की कार्यक्षमता जोड़ने के लिए मुझे क्या करने की ज़रूरत है?

वर्तमान में, जब मैं लॉग इन करने और एनएवी बार लिंक पर क्लिक करने का प्रयास करता हूं, तो मैं प्रोफाइल पेज पर जाना चाहता हूं। इसके बजाए, मैं एक त्रुटि संदेश पर जाता हूं जो कहता है कि जिस पृष्ठ को मैं ढूंढ रहा हूं वह मौजूद नहीं है। जिस पथ को खोजना है वह प्रोफ़ाइल पर शुरू होता है (उपयोगकर्ता/प्रोफ़ाइल के बजाए) - सुनिश्चित नहीं है कि यह एक सुराग है।

मेरे रास्ते हैं:

प्रोफाइल:

profiles GET  /profiles(.:format)     profiles#index 
         POST  /profiles(.:format)     profiles#create 
      new_profile GET  /profiles/new(.:format)    profiles#new 
      edit_profile GET  /profiles/:id/edit(.:format)   profiles#edit 
       profile GET  /profiles/:id(.:format)    profiles#show 
         PATCH  /profiles/:id(.:format)    profiles#update 
         PUT  /profiles/:id(.:format)    profiles#update 
         DELETE /profiles/:id(.:format)    profiles#destroy 

उपयोगकर्ता:

user_password POST  /users/password(.:format)    devise/passwords#create 
     new_user_password GET  /users/password/new(.:format)   devise/passwords#new 
     edit_user_password GET  /users/password/edit(.:format)   devise/passwords#edit 
         PATCH  /users/password(.:format)    devise/passwords#update 
         PUT  /users/password(.:format)    devise/passwords#update 
cancel_user_registration GET  /users/cancel(.:format)    users/registrations#cancel 
     user_registration POST  /users(.:format)      users/registrations#create 
    new_user_registration GET  /users/sign_up(.:format)    users/registrations#new 
    edit_user_registration GET  /users/edit(.:format)     users/registrations#edit 
         PATCH  /users(.:format)      users/registrations#update 
         PUT  /users(.:format)      users/registrations#update 
         DELETE /users(.:format)      users/registrations#destroy 
     user_confirmation POST  /users/confirmation(.:format)   devise/confirmations#create 
    new_user_confirmation GET  /users/confirmation/new(.:format)  devise/confirmations#new 
         GET  /users/confirmation(.:format)   devise/confirmations#show 
      user_unlock POST  /users/unlock(.:format)    devise/unlocks#create 
     new_user_unlock GET  /users/unlock/new(.:format)   devise/unlocks#new 
         GET  /users/unlock(.:format)    devise/unlocks#show 
      finish_signup GET|PATCH /users/:id/finish_signup(.:format)  users#finish_signup 

नई प्रयास:

resources :users do 
    resources :profile 
    end 
: इसके अलावा नीचे दिए गए सुझावों को, मैं के रूप में संसाधनों नेस्ट

और मैं रीडायरेक्ट करने के लिए बदल: NoMethodError (अपरिभाषित विधि `प्रोफ़ाइल 'शून्य के लिए: NilClass):

अगला प्रयास:

मैं ऊपर के सभी रखने

def self.provides_callback_for(provider) 
    class_eval %Q{ 
     def #{provider} 
     @user = User.find_for_oauth(env["omniauth.auth"]) 

     if @user.persisted? 
      sign_in_and_redirect [current_user, current_user.profile || current_user.build_profile] 

      set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format? 
     else 
      session["devise.#{provider}_data"] = env["omniauth.auth"] 
      redirect_to new_user_registration_url 
     end 
     end 
    } 
    end 

मैं इस त्रुटि मिलती है , लेकिन इतना तरह रीडायरेक्ट की जगह का प्रयास करें:

def self.provides_callback_for(provider) 
    class_eval %Q{ 
     def #{provider} 
     @user = User.find_for_oauth(env["omniauth.auth"]) 

     if @user.persisted? 
      sign_in_and_redirect [@user, @user.profile || @user.build_profile] 

      set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format? 
     else 
      session["devise.#{provider}_data"] = env["omniauth.auth"] 
      redirect_to new_user_registration_url 
     end 
     end 
    } 
    end 

RuntimeError (Could not find a valid mapping for [#<User id: 1 ... 

और फिर इसे उपयोगकर्ता तालिका में सभी विशेषताओं को सूचीबद्ध करता है, और उसके बाद पता चलता है:

#<Profile id: nil, user_id: 1, 

प्रोफाइल तालिका में सभी विशेषताओं के बाद।

एक नया प्रयास

मैं इस पोस्ट पाया: मेरे उपयोगकर्ता मॉडल के लिए

after_create do 
    create_user_profile 
    end 

: Rails Trying to create a profile after the user has been created

और मैं जोड़ने की कोशिश की।

मैंने रीडायरेक्ट को वैसे ही रखा जैसा मैंने किया था (एलेक्सी के सुझाव के बाद) और फिर से प्रयास किया।

RuntimeError (Could not find a valid mapping for [#<User id: 1 

किसी को भी यह कैसे हल करने के लिए देख सकते हैं:

मैं एक ही त्रुटि संदेश मिलता है?

एक और प्रयास: Ruby on Rails - Creating a profile when user is created

जो रेल 4 (जो मैं का उपयोग करें) के लिए कहते हैं, मैं केवल यह कॉलबैक की जरूरत है::

after_create: create_profile

मैं तो इस पोस्ट पाया

जब मैं इसे आज़माता हूं, तो मुझे ऊपर की तरह ही त्रुटि मिलती है।

एक आईडिया:

मैं अपनी प्रोफ़ाइल नियंत्रक के लिए मचान जनरेटर का इस्तेमाल किया।

मेरे विधि बनाने है:

def create 
    @profile = Profile.new(profile_params) 

    respond_to do |format| 
     if @profile.save 
     format.html { redirect_to @profile } 
     format.json { render :show, status: :created, location: @profile } 
     else 
     format.html { render :new } 
     format.json { render json: @profile.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

इस त्रुटि हो सकता है प्रयोक्ता आईडी विशेष रूप से इस विधि में संदर्भित नहीं किया जा रहा के साथ कुछ है। मैंने प्रोफ़ाइल नियंत्रक के लिए मजबूत पैरा में user_id whitelisted किया है।

एक नया प्रयास

यह लेख create_profile के बजाय build_profile का उपयोग कर पता चलता है।

https://stackoverflow.com/questions/8337682/create-another-model-upon-user-new-registration-in-devise 

मैं

after_create :build_profile 

करने के लिए अपने user.rb बदल जब मैं इस बचाने के लिए और मैं एक ही त्रुटि मिलती है की कोशिश करो।

एक और प्रयास

इस पोस्ट में मैट के जवाब से पता चलता है मैं के रूप में build_profile की एक परिभाषा शामिल करने के लिए है:

after_create :build_profile 

    def build_profile 
    Profile.create(user: self) # Associations must be defined correctly for this syntax, avoids using ID's directly. 
    end 

मैं अपने user.rb बदल दिया है:

after_create :build_profile 

    def build_profile 
    Profile.create(user: self) # Associations must be defined correctly for this syntax, avoids using ID's directly. 
    end 

जब मैं इसे सहेजता हूं और कोशिश करता हूं, मुझे एक ही त्रुटि मिलती है, लेकिन अधिक जानकारी के साथ। लॉग अभी भी दिखाई देते हैं:

SQL (2.9ms) INSERT INTO "profiles" ("user_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["user_id", 1], ["created_at", "2015-12-18 21:58:49.993866"], ["updated_at", "2015-12-18 21:58:49.993866"]] 
2015-12-18T21:58:49.966648+00:00 app[web.1]: Profile Load (1.5ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 LIMIT 1 [["user_id", 1]] 
2015-12-18T21:58:50.007428+00:00 app[web.1]: Completed 500 Internal Server Error in 113ms (ActiveRecord: 21.8ms) 
2015-12-18T21:58:49.941385+00:00 app[web.1]: User Load (1.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]] 

इस प्रस्ताव समस्या पर कोई प्रकाश करता है:

RuntimeError (Could not find a valid mapping for [#<User id: 1, 

लेकिन उस त्रुटि संदेश के सभी के बाद, यह जानकारी के इस नए टुकड़ा देता है?

अद्यतन

मैं यह फिर से कोशिश की (कोड में कोई बदलाव किए बिना)। अब मैं कोई त्रुटि मिलती है, लेकिन इस बार लॉग कहते हैं:

RuntimeError (Could not find a valid mapping for [#<User id: 1, first_name: "Me", last_name: "Ma", email: "[email protected]", encrypted_password: "$2a$jqQn..HSvlcNtfAH/28.DQoWetO...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 16, current_sign_in_at: "2015-12-15 09:57:14", last_sign_in_at: "2015-12-15 09:27:07", current_sign_in_ip: #<IPAddr: IPv4:49.191.55>, last_sign_in_ip: #<IPAddr: IPv4:49.191.135.255.255>, confirmation_token: nil, confirmed_at: "2015-12-15 09:02:58", confirmation_sent_at: "2015-12-15 08:42:48", unconfirmed_email: nil, failed_attempts: 0, unlock_token: nil, locked_at: nil, created_at: "2015-12-09 23:53:20", updated_at: "2015-12-15 09:57:14", image: nil>, #<Profile id: 1, user_id: 1, title: nil, hero_image: nil, overview: nil, occupation: nil, external_profile: nil, working_languages: nil, created_at: "2015-12-18 21:58:49", updated_at: "2015-12-18 21:58:49">]): 

इस बार अंतर यह प्रोफाइल पहले 1. की एक प्रयोक्ता आईडी कुंजी है कि, इस शून्य था।

+0

कैसे आप 'रेक routes' उपयोगकर्ता और प्रोफ़ाइल रास्तों की तलाश में है का उपयोग कर सकते हैं? :) – davidwessman

+0

हाय डेविड, मैंने उन्हें – Mel

+0

से ऊपर कॉपी किया "जिस पथ को यह देखने के लिए खोज रहा है (उपयोगकर्ता/प्रोफ़ाइल के बजाए) - यह सुनिश्चित नहीं है कि यह एक सुराग है।" क्या आप प्रत्येक उपयोगकर्ता को एक अद्वितीय प्रोफ़ाइल से लिंक करना चाहते हैं? या क्या आप पहली प्रोफ़ाइल प्राप्त करने के लिए 'डोमेन/प्रोफ़ाइल/1' पर जाने में सक्षम होना चाहते हैं? – davidwessman

उत्तर

0

आपने अपने कॉलबैक नियंत्रक में sing_in_and_redirect_user जोड़ा है। आपके द्वारा रीडायरेक्ट किए गए पथ को संपादित करने के लिए after_sign_in_path_for को अपने applications_controller में ओवरराइड और संपादित करना चाहिए।

class ApplicationController < ActionController::Base 
    def after_sign_in_path_for(resource) 
    request.env['omniauth.origin'] || stored_location_for(resource) || root_path 
    end 
end 

यहां आप वास्तविक पथ को रीडायरेक्ट कर सकते हैं। Omniauth भाग के बारे में निश्चित नहीं है, लेकिन आप profile_path(resource.profile) जोड़ सकते हैं।

संदर्भ: https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in

1

आप नेस्ट संसाधनों

resources :users do 
    resource :profile 
end 

और फिर

redirect_to [current_user, current_user.profile || current_user.build_profile] 
+0

से नीचे एलेक्सी का सुझाव है हाय - मैंने यह कोशिश की, लेकिन यह त्रुटि प्राप्त करें: NoMethodError (शून्य के लिए अपरिभाषित विधि 'प्रोफ़ाइल': नील क्लास): 2015-12-15T10: 33: 09.278064 + 00: 00 ऐप [वेब .1] : – Mel

+0

यह बताता है कि आपके पास current_user (current_user == nil) नहीं है। यदि इसे परिभाषित किया गया है तो @user का प्रयोग करें। –

+0

हाय एलेक्सी। मैं devise का उपयोग करें। मेरे पास current_user सहायक उपलब्ध है। साथ ही, क्या आप सुनिश्चित हैं कि यह उपयोगकर्ता के बारे में बात कर रहा है, जब विधि प्रोफ़ाइल कहती है? सहायता – Mel

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