如何在 rails 应用程序中访问 profiles/show.html.erb
How can I access the profiles/show.html.erb in rails app
我正在使用 Devise,注册后,我希望我的用户可以访问 profiles/show.html.erb
我有一个registrations_controller.rb
,里面有这段代码
def after_sign_up_path_for(resource)
new_user_profile_path(current_user)
end
它将用户定向到 profiles/new.html.erb
但我希望用户转到 profiles/show.html.erb
rake routes
显示此路径:
new_user_profile GET /users/:user_id/profile/new(.:format) profiles#new
edit_user_profile GET /users/:user_id/profile/edit(.:format) profiles#edit
GET /users/:user_id/profile(.:format) profiles#show
我如何修改这段代码以指向 profiles/show.html.erb
?
我没有经验自己解决这个问题,任何帮助都会很棒
这是我的routes.rb
文件
Rails.application.routes.draw do
devise_for :users, :controllers => { registrations: 'registrations' }
resources :users do
resource :profile
end
root 'pages#index'
end
** 编辑**
the rake routes output
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
user_profile POST /users/:user_id/profile(.:format) profiles#create
new_user_profile GET /users/:user_id/profile/new(.:format) profiles#new
edit_user_profile GET /users/:user_id/profile/edit(.:format) profiles#edit
GET /users/:user_id/profile(.:format) profiles#show
PATCH /users/:user_id/profile(.:format) profiles#update
PUT /users/:user_id/profile(.:format) profiles#update
DELETE /users/:user_id/profile(.:format) profiles#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
GET /%20/users/:user_id/profile(.:format)%20 profiles#show
root GET / pages#index
您需要在您的控制器中创建
def show
end
您也可以尝试将注册控制器中的代码替换为。
def after_sign_up_path_for(resource)
show_user_profile_path(current_user)
end
从资源中删除 'do':用户在 routes.rb 文件中执行。
def after_sign_up_path_for(resource)
user_path(current_user)
end
rake routes
显示 user GET /users/:id(.:format) users#show
我修好了,检查你的github。
您的资源拼写错误
应该是 resources :profile
而不是 resource :profile
这就是为什么它没有通过 rake routes
命令向您显示配置文件资源的路径。
我在 profile_controller.rb 文件
的显示操作中使用了 profile_path(current_user)
routes.rb
get 'profiles/show', to: 'profiles/show', as: :profile
profiles_controller.rb
def show
unless params[:user_id] @user = User.find(current_user.id)
@user = User.find(params[:user_id])
end
然后
def after_sign_up_path_for(resource)
profile_path
end
我正在使用 Devise,注册后,我希望我的用户可以访问 profiles/show.html.erb
我有一个registrations_controller.rb
,里面有这段代码
def after_sign_up_path_for(resource)
new_user_profile_path(current_user)
end
它将用户定向到 profiles/new.html.erb
但我希望用户转到 profiles/show.html.erb
rake routes
显示此路径:
new_user_profile GET /users/:user_id/profile/new(.:format) profiles#new
edit_user_profile GET /users/:user_id/profile/edit(.:format) profiles#edit
GET /users/:user_id/profile(.:format) profiles#show
我如何修改这段代码以指向 profiles/show.html.erb
?
我没有经验自己解决这个问题,任何帮助都会很棒
这是我的routes.rb
文件
Rails.application.routes.draw do
devise_for :users, :controllers => { registrations: 'registrations' }
resources :users do
resource :profile
end
root 'pages#index'
end
** 编辑**
the rake routes output
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
user_profile POST /users/:user_id/profile(.:format) profiles#create
new_user_profile GET /users/:user_id/profile/new(.:format) profiles#new
edit_user_profile GET /users/:user_id/profile/edit(.:format) profiles#edit
GET /users/:user_id/profile(.:format) profiles#show
PATCH /users/:user_id/profile(.:format) profiles#update
PUT /users/:user_id/profile(.:format) profiles#update
DELETE /users/:user_id/profile(.:format) profiles#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
GET /%20/users/:user_id/profile(.:format)%20 profiles#show
root GET / pages#index
您需要在您的控制器中创建
def show
end
您也可以尝试将注册控制器中的代码替换为。
def after_sign_up_path_for(resource)
show_user_profile_path(current_user)
end
从资源中删除 'do':用户在 routes.rb 文件中执行。
def after_sign_up_path_for(resource)
user_path(current_user)
end
rake routes
显示 user GET /users/:id(.:format) users#show
我修好了,检查你的github。
您的资源拼写错误
应该是 resources :profile
而不是 resource :profile
这就是为什么它没有通过 rake routes
命令向您显示配置文件资源的路径。
我在 profile_controller.rb 文件
profile_path(current_user)
routes.rb
get 'profiles/show', to: 'profiles/show', as: :profile
profiles_controller.rb
def show
unless params[:user_id] @user = User.find(current_user.id)
@user = User.find(params[:user_id])
end
然后
def after_sign_up_path_for(resource)
profile_path
end