我正在使用Devise,并试图允许每个用户创建1个配置文件。我可以将新注册的用户发送到他们可以创建配置文件的页面,但一旦用户注销并重新登录,就不会转到配置文件显示页面。
换句话说-
我可以注册一个新用户并将该用户发送到“创建配置文件”页面,然后我可以与新用户一起创建配置文件(我不确定配置文件是否正确保存)。。。注销并登录后,我收到错误:
ActiveRecord::RecordNotFound in ProfilesController
Couldn't find Profile without an ID
我希望用户被发送到他们的个人资料显示页面。。。
对这个问题有什么想法吗?
代码(按文件排序)如下
用户.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
has_one :profile
end
个人资料.rb
class Profile < ActiveRecord::Base
attr_accessible :first_name, :last_name
belongs_to :user
end
配置文件_控制器.rb
class ProfilesController < ApplicationController
def index
@profiles = Profile.all
respond_to do |format|
format.html
format.json { render json: @profiles }
end
end
def show
@profile = Profile.find(params[:id])
respond_to do |format|
format.html
format.json { render json: @profile }
end
end
def new
@profile = Profile.new
respond_to do |format|
format.html
format.json { render json: @profile }
end
end
def edit
@profile = Profile.find(params[:id])
end
def create
@profile = Profile.new(params[:profile])
respond_to do |format|
if @profile.save
format.html { redirect_to @profile, notice: 'Profile was successfully created.' }
format.json { render json: @profile, status: :created, location: @profile }
else
format.html { render action: "new" }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
def update
@profile = Profile.find(params[:id])
respond_to do |format|
if @profile.update_attributes(params[:profile])
format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
def destroy
@profile = Profile.find(params[:id])
@profile.destroy
respond_to do |format|
format.html { redirect_to profiles_url }
format.json { head :no_content }
end
end
end
注册控制器.rb
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
request.env['omniauth.origin'] || stored_location_for(resource) || new_profile_path
end
end
应用程序控制程序.rb
class ApplicationController < ActionController::Base
def after_sign_in_path_for(resource)
request.env['omniauth.origin'] || stored_location_for(resource) || show_path(resource.profile)
end
end
路线.rb
BaseApp::Application.routes.draw do
resources :profiles
get "users/show"
devise_for :users, :controllers => { :registrations => "registrations" }
resources :users
match '/show', to: 'profiles#show'
match '/signup', to: 'users#new'
root to: 'static_pages#home'
match '/', to: 'static_pages#home'
â¦
end