我希望有人可以提供帮助。我正在使用Devise gem来注册和登录用户。我有一个配置文件控制器。当现有用户登录时,我希望他们转到Profile的show.html.erb页面以查看其配置文件。我希望这将在会话控制下进行,但它似乎并没有做任何事情无法重定向设计标志页以显示配置文件
会话控制器代码:
class Registrations::SessionsController < Devise::SessionsController
# before_action :configure_sign_in_params, only: [:create]
protected
def after_sign_in_path_for(resource)
profile_path(resource)
end
然而,当用户注册时,重定向下顺利工作注册控制器如下:
class RegistrationsController < Devise::RegistrationsController
# before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]
protected
def after_sign_up_path_for(resource)
new_profile_path(resource)
end.
我也想有一个链接到用户个人资料页面,当他们在登录,但是当我这样做,抛出了以下错误
的链接application.html.erb代码如下(我试图替代“@profile”的,但没有成功许多不同的变量)
<li><%= link_to 'Show Profile', profile_path(@profile), :class => 'navbar-link' %></li>
我收到的错误是:
ActionController::UrlGenerationError in Profiles#index
No route matches {:action=>"show", :controller=>"profiles", :id=>nil} missing required keys: [:id]
我的路线(这我不知道是设置正确:
Rails.application.routes.draw do
resources :profiles
get 'profiles/:id', to: 'profiles#show'
get '/profiles/new' => 'profiles#new'
get '/profiles/edit' => 'profiles#edit'
get '/profiles/index' => 'profiles#index'
root to: 'pages#index'
devise_for :users, :controllers => { :registrations => "registrations" }
最后,我的档案控制器:
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
def index
@search = Profile.search(params[:q])
@profiles = @search.result(distinct: true)
end
def show
@profile = Profile.find(params[:id])
end
def new
@profile = Profile.new
end
def create
@profile = Profile.new(profile_params)
respond_to do |format|
if @profile.save
format.html { redirect_to @profile, notice: 'Your Profile was successfully created' }
format.json { render :show, status: :created, location: @profile }
else
format.html { render :new }
format.json { render json: @profile.errors, status: :unprocessable_entry }
end
end
end
def edit
@profile = Profile.find(params[:id])
end
def update
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :edit }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
def destroy
@profile.destroy
respond_to do |format|
format.html { redirect_to profile_url, notice: 'Profile was successfully destroyed.' }
format.json { head :no_content }
end
end
def set_profile
@profile = Profile.find(params[:id])
end
private
def profile_params
params[:profile][:user_id] = current_user.id
params.require(:profile).permit(:full_name, :contact_number, :location, :makeup_type, :bio, :user_id, :image)
end
end
任何帮助最受赞赏。
直接重定向到配置文件,所以我将其标记为正确的路由。我是否应该在应用程序控制器中声明我的after_sign_up_path_for(资源)而不是注册?这将是一个更好的做事方式,为什么? – marg08