私はこの投稿を整理しようとしました。それには、以下の回答の中で他の人が提案したことに起因するエラーも含まれます。Rails 4 - 新しいユーザーが作成したときに自動的にプロファイルを作成する
私は工夫とomniauthを使用Railsの4
でアプリを作るしようとしています。
私はユーザーとプロファイルのモデルを持っています。ユーザーには1つのプロファイルがあり、プロファイルはユーザーに属します。
私の目的は、ユーザーが実際に触れていないすべてのものに対してユーザーモデルを使用することです。私はユーザーが維持しているもののためのプロファイルモデルを使用します。
新しいユーザーが作成されると、そのユーザーはプロファイルページにリダイレクトされ、ユーザーモデルで作成された詳細が部分的に入力されます。
困っています。
私が持っている:私はまた、コールバックリダイレクトで試してみました
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>
私も試してみました
<% if user_signed_in? %>
Hi <%= link_to(current_user.first_name.titlecase, profile_path(current_user)) %></span>
_nav.html.erb:
_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
: さらに以下の提案に、私は、などのリソースを入れ子になりました
と私はにリダイレクトを変更:
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
私はこのエラーを取得:NoMethodError(nilのための未定義のメソッド `プロファイル」: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 [@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,
に続き、プロファイルテーブルのすべての属性が続きます。
A NEW試行
私はこの記事が見つかりました:Rails Trying to create a profile after the user has been created
をそして私は追加しようとした:
after_create do
create_user_profile
end
を私のユーザモデルに。
私はそれを持っていたのと同じリダイレクトを続けました(アレクセイの提案に従ってください)。もう一度試しました。
私は、同じエラーメッセージが表示されます。
誰もがこの問題を解決する方法を見ることができますか?
FURTHER試み:(私が使用して)Railsの4のために言う Ruby on Rails - Creating a profile when user is created
、私はこのコールバックが必要です:
after_create:create_profile
をは、私は、この記事を見つけました
これを試してみると、上記と同じエラーが発生します。
IDEA:
私は私のプロフィールコントローラ用の足場ジェネレータを使用。
私のメソッドを作成することです:
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
は多分、特にこの方法で参照されていないユーザーIDとは何か、このエラーです。プロファイルコントローラの強力なパラメータでuser_idをホワイトリストに登録しました。
新しい試み
この記事ではなくcreate_profileのbuild_profileを使用することを提案しています。
https://stackoverflow.com/questions/8337682/create-another-model-upon-user-new-registration-in-devise
私はこれを保存し、私は同じエラーを取得しようとすると、私は
after_create :build_profile
に私のuser.rbを変えました。この記事で
別の試み
Mattの答えは私のように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
:
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を変更しました私はこれを保存して試してみますが、同様のエラーが発生しますが、より多くの情報があります。
RuntimeError (Could not find a valid mapping for [#<User id: 1,
しかし、そのエラー・メッセージのすべての後に、それはこの情報の新しい作品を提供します:ログがまだ表示さ
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]]
は問題上の任意の光このオファーしていますか?
UPDATE
Iは、(コードを変更せずに)再びこの試み。今、私はエラーを取得するが、今回はログは言う:
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のユーザIDキーを持っている、これはゼロだったということです。
がどのように 'すくいroutes'ユーザーとプロファイルのパスを探していますか? :) – davidwessman
こんにちはデビッド、私はそれらを – Mel
の上にコピーしました "探しているパスは、(ユーザー/プロフィールではなく)プロファイルで始まります。 各ユーザーに固有のプロファイルへのリンクを設定しますか? 「domain/profile/1」にアクセスして最初のプロファイルを取得できますか? – davidwessman