Я надеюсь, что кто-то может помочь. Я использую драгоценный камень Devise для регистрации и подписания пользователей. У меня есть контроллер профиля. Когда существующий пользователь входит в систему, я хочу, чтобы они были перенаправлены на страницу show.html.erb Profile, чтобы просмотреть их профиль. Я бы ожидать, что это будет сделано в рамках контроллера Sessions, но не похоже, чтобы сделать что-нибудьНевозможно перенаправить Devise sign page, чтобы показать профиль
Код Сессии контроллера:
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
Любая помощь очень ценится.
Это перенаправлено прямо в профиль, поэтому я отметил его как правильное относительно его маршрутизации. Должен ли я объявить my after_sign_up_path_for (ресурс) в контроллере приложения также вместо регистраций? Что было бы лучшим способом сделать что-то и почему? – marg08