带有链接的静态页面
<h1>Dashboard</h1>
<%= link_to 'Dashboard', :action => :dashboard %> |
<%= link_to 'Ask for help', :controller => :tasks, :action => :new %> |
<%= link_to 'Profile', profile_path %>| #relevant line
配置文件控制器
class ProfilesController < ApplicationController
def new
@profile = Profile.new
end
def create
@profile = Profile.new(params[:profile])
@profile.user_id = current_user.id
if @profile.save
redirect_to static_pages_dashboard_path
end
end
def edit
@profile = Profile.find(params[:id])
end
def show
@profile = Profile.current_user
end
end
routes.rb
Test::Application.routes.draw do
get "welcome/index"
get "static_pages/home"
get "static_pages/dashboard"
get "tasks/index"
root "static_pages#home"
devise_for :users
resources :tasks
resources :profiles
end
我正在尝试制作一个单独的模型/控制器来处理用户的个人信息,称为profile
。
尝试show
从仪表板视图链接到操作/视图会导致错误:
StaticPages#dashboard中的ActionController :: UrlGenerationError
没有路由与{:controller =>“ profiles”,:action =>“ show”}匹配,缺少必需的键:[: id]
我该如何解决?
谢谢!
您应该传递个人资料ID,以使个人资料显示网址生成成为可能(否则,Rails不知道您要链接到哪个个人资料):
<%= link_to 'Profile', profile_path(current_user.profile) %>
当然,您需要确保设置了关联,并且用户始终具有关联的配置文件。或者,您可以在呈现链接之前检查配置文件是否存在,如下所示:
<%= link_to 'Profile', profile_path(current_user.profile) if current_user.profile %>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句