【问题标题】:Can't redirect Devise sign page to show profile无法重定向设计标志页面以显示配置文件
【发布时间】: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


    【解决方案1】:

    好的,有两个问题:

    1. 登录后重定向
    2. 应用程序布局中的 URL 生成错误

    登录后重定向

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

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

    应用布局中的网址生成错误

    我假设配置文件模型与用户相关联(例如配置文件属于_to 用户,或者可能是用户 has_one 配置文件)。我还假设您希望获得当前用户个人资料的链接。

    如果是这种情况,那么您很可能会这样做:

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

    否则,您应该在应用程序控制器或任何使用应用程序布局的控制器中将@profile 设置为before_action

    【讨论】:

    • 非常感谢。上面的链接有效。所以谢谢。至于登录后的重定向,我在添加上面的路由时似乎遇到了问题。我认为这更多地与我在整个申请过程中如何声明事物有关。我不知道。最后,我按照下面 Gurmukh 的回答将 after_sign_in_path_for 放置在应用程序控制器中。我给你加了几分。再次感谢。
    【解决方案2】:

    在你的应用控制器中你想要这样的东西

    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
    

    【讨论】:

    • 这直接重定向到配置文件,因此我已将其标记为关于路由的正确。我是否应该在应用程序控制器中声明我的 after_sign_up_path_for(resource) 而不是注册?哪种方式更好?为什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多