我正在使用设计并试图允许每个用户创建 1 个配置文件。我可以将新注册的用户发送到他们可以创建个人资料的页面,但是一旦用户注销并重新登录,它将不会进入个人资料显示页面。
换句话说-
我可以注册一个新用户并将用户发送到创建配置文件页面,然后我可以使用新用户创建一个配置文件(我不确定配置文件是否正确保存)......在我注销并登录后,我收到了错误:
ActiveRecord::RecordNotFound in ProfilesController#show
Couldn't find Profile without an ID
我希望将用户发送到他们的个人资料显示页面...
对这个问题有什么想法吗?
代码(按文件排序)如下……</p>
用户.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
profile_controller.rb
class ProfilesController < ApplicationController
# GET /profiles
# GET /profiles.json
def index
@profiles = Profile.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @profiles }
end
end
# GET /profiles/1
# GET /profiles/1.json
def show
@profile = Profile.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @profile }
end
end
# GET /profiles/new
# GET /profiles/new.json
def new
@profile = Profile.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @profile }
end
end
# GET /profiles/1/edit
def edit
@profile = Profile.find(params[:id])
end
# POST /profiles
# POST /profiles.json
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
# PUT /profiles/1
# PUT /profiles/1.json
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
# DELETE /profiles/1
# DELETE /profiles/1.json
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
application_controller.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