【发布时间】:2016-11-29 01:07:57
【问题描述】:
我希望有人可以提供帮助。我正在使用 Devise gem 来注册和登录用户。我有一个配置文件控制器。当现有用户登录时,我希望他们被转移到个人资料的 show.html.erb 页面以查看他们的个人资料。我希望这将在 Sessions 控制器下完成,但它似乎没有做任何事情
Sessions 控制器代码是:
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
非常感谢任何帮助。
【问题讨论】:
标签: ruby-on-rails ruby devise routes