我正在用一个控制器“项目”制作一个站点,并且我想显示所有与路由有关的项目:
我试过了
得到'front /:id'=>'projects#show',:constraints => {:id => / [^ /] + /}在route.rb中,但是它不起作用。
我的文件 :
routes.rb
Rails.application.routes.draw做 资源:用户,路径:“ / admin / clients” 得到'admin'=>'admin#dashbord' 获取“管理员/个人资料” 获取“管理员/设置” 得到'admin / _admin_header' 得到'front'=>'front#index' 获取'front / profile'=>'front#profile' 得到'front /:id'=>'projects#show' 范围'/ admin'做 资源:项目办 资源:图片 结尾 结尾 结尾
projects_controller.rb
布局“ adminApplication” before_action:set_project,仅:[:show,:edit,:update,:destroy] 定义指数 @projects = Project.all 结尾 def秀 结尾 def新 @project = Project.new 结尾 def编辑 结尾 def创建 @project = Project.new(project_params) response_do |格式| 如果@ project.save format.html {redirect_to @project,注意:“项目已成功创建。” } format.json {渲染:显示,状态::创建,位置:@project} 别的 format.html {render:new} format.json {渲染json:@ project.errors,状态::unprocessable_entity} 结尾 结尾 结尾 def更新 response_do |格式| 如果@ project.update(project_params) format.html {redirect_to @project,注意:“项目已成功更新。” } format.json {渲染:显示,状态::ok,位置:@project} 别的 format.html {render:edit} format.json {渲染json:@ project.errors,状态::unprocessable_entity} 结尾 结尾 结尾 def破坏 @ project.destroy response_do |格式| format.html {redirect_to projects_url,注意:“项目已成功销毁。” } format.json {head:no_content} 结尾 结尾 私人的 def set_project @project = Project.find(params [:id]) 结尾 def project_params params.require(:project).permit(:name,:date,:location,:integer) 结尾 结尾
front_controller.rb
定义指数 @projects = Project.all 渲染“项目/索引” 结尾 def秀 结尾 def配置文件 结尾 结尾
在projects / index.html.erb中
-link_to'Show',项目 -link_to'显示',front_path(项目)
我已经检查了所有类似的问题。
谢谢你的帮助 !
风设计
更新
rake routes | grep front
:
front GET /front(.:format) front#index
front_profile GET /front/profile(.:format) front#profile
GET /front/:id(.:format) projects#show
您正在使用命名的路由助手,但未指定它:
- link_to 'Show', front_path(project)
您会在路线中看到不存在的front_path
for projects#show
:
rake routes | grep front
front GET /front(.:format) front#index
front_profile GET /front/profile(.:format) front#profile
GET /front/:id(.:format) projects#show
因此,在您routes.rb
添加帮助器时:
get 'front/:id' => 'projects#show', as: :custom_front
现在运行rake routes
并查看新的帮助程序(应该为custom_front_path
)并使用它:
- link_to 'Show', custom_front_path(project)
请参阅文档中的更多信息-4.3覆盖命名助手
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句