2016-11-29 23 views
0

我希望有人可以提供帮助。我正在使用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 

任何帮助最受赞赏。

回答

0

在您的应用程序控制器,你想是这样的

class ApplicationController < ActionController::Base 
    protect_from_forgery with: :exception 

    def after_sign_in_path_for(user) 
    profile_path(current_user) 
    end 
    #user is the model name for example that you created with devise 

end 
+0

直接重定向到配置文件,所以我将其标记为正确的路由。我是否应该在应用程序控制器中声明我的after_sign_up_path_for(资源)而不是注册?这将是一个更好的做事方式,为什么? – marg08

1

好了,所以这里有两个问题:标志后

  1. 重定向在
  2. URL生成错误登录后

重定向应用布局

您需要将控制器添加到您的路由定义中(就像您有注册一样)

devise_for :users, controllers: { registrations: "registrations", sessions: 'registrations/sessions' } 

在应用布局URL生成错误

我假定轮廓模型与用户相关联(例如配置文件belongs_to用户,或者用户has_one配置文件)。我还假设你想为当前用户的配置文件建立一个链接。

如果是这样的话,那么你可以最有可能做这样的事情:

<%= if current_user %> 
    <li> 
    <%= link_to 'Show Profile', profile_path(current_user.profile), :class => 'navbar-link' %> 
    </li> 
<% end %> 

否则,你应该在应用程序控制器或在使用应用程序布局的任何控制器设置@profile在一些before_action

+0

非常感谢。上面的链接工作。所以谢谢。至于登录后重定向,我在添加上面的路由时似乎遇到了问题。我认为这更多的是关于我如何在应用程序中宣布的东西。我不确定。最后,我根据Gurmukh的回答,将after_sign_in_path_for放在应用程序控制器中。我已经标记了几个。再次感谢。 – marg08

相关问题