将用户(设计)连接到他们的个人资料

时间:2012-09-24 23:31:00

标签: ruby-on-rails ruby devise profile

我正在使用Devise,我正在尝试允许每个用户创建1个配置文件。我可以将新注册的用户发送到他们可以创建个人资料的页面,但是一旦用户退出并重新登录,就不会进入个人资料显示页面。

换句话说 -

我可以注册新用户并将用户发送到创建个人资料页面,然后我可以使用新用户创建个人资料(我不确定个人资料是否正确保存)...我退出后签名在我收到错误:

ActiveRecord::RecordNotFound in ProfilesController#show

Couldn't find Profile without an ID

我希望将用户发送到他们的个人资料显示页面...

有关此问题的任何想法?

代码(按文件排序)低于......

user.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

profile.rb

class Profile < ActiveRecord::Base
 attr_accessible :first_name, :last_name

 belongs_to :user

end

profiles_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

registrations_controller.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

的routes.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

2 个答案:

答案 0 :(得分:1)

在您的控制器中,您使用以下代码@profile = Profile.find(params[:id])。登录params[:id]时必须为零。

在您创建后重定向时,这不是零,因为您在此处{id} {}发送了一个ID。这转化为redirect_to @profile。当您使用/ match路径时,没有id。

因此,一个解决方案是在ProfileController的show动作中使用帮助器redirect_to profile_path(@profile)。将current_user替换为@profile = Profile.find(params[:id])。这可能会改变您所需的功能,因为它需要用户登录。这将保留数学路径(/ show url)。它起作用,因为它不再依赖于id。

您也可以将@profile = current_user.profile更改为show_path(resource.profile)。这将使用资源配置文件路径和url / profiles /:id而不是show /你可能正在寻找。

答案 1 :(得分:0)

回答@Phil,我解决了项目中的另一个问题。谢谢\ o /

  • ruby​​ 2.0.0p247(2013-06-27修订版41674)[x86_64-linux]
  • Rails 4.0.0

你的情况,我这样解决了:

在用户和个人资料模型中添加inverse_of:

<强> user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable

  has_one :profile, inverse_of: :user
end

<强> profile.rb

class Profile < ActiveRecord::Base
  belongs_to :user, inverse_of: :profile
  validates :first_name, :user_id, :presence => true
  validates :gender, :inclusion => {:in => %w(M F)}

end

application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  # redirect user after login
  def after_sign_in_path_for(resource)
    unless current_user.profile.nil?
      profiles_path 
    else
      flash[:alert] = "Please complete your profile"
      new_profile_path
    end
  end

  # redirect after logout
  def after_sign_out_path_for(resource_or_scope)
    new_user_session_path
  end

end

这对我有用,我希望这有帮助