【问题标题】:Rails Dot in Url with two routes for one controllerURL中的Rails Dot,一个控制器有两条路线
【发布时间】:2016-07-07 20:25:54
【问题描述】:

我正在用一个控制器“项目”创建一个站点,我想显示所有带有路由的项目:

  • /admin/projects/:id = /admin/projects/1(有效)
  • /front/:id = /front.1(不起作用)

我试过了

get 'front/:id' => 'projects#show', :constraints => { :id => /[^/]+/ }
在 route.rb 但它不起作用。

我的文件:

routes.rb

Rails.application.routes.draw 做

  资源:用户,路径:'/admin/clients'

  得到'admin' =>'admin#dashbord'

  获取“管理员/个人资料”

  获取“管理员/设置”

  获取“管理员/_admin_header”

  得到“前面”=>“前面#index”

  得到'前/配置文件' => '前#配置文件'

  得到 'front/:id' => 'projects#show'

  范围 '/admin' 做
    资源:项目做
      资源:图片
    结尾
  结尾

  结尾

projects_controller.rb

布局'adminApplication' before_action :set_project,仅:[:show, :edit, :update, :destroy] 定义索引 @projects = Project.all 结尾 定义显示 结尾 定义新 @project = Project.new 结尾 定义编辑 结尾 定义创建 @project = Project.new(project_params) respond_to 做 |格式| 如果@project.save format.html { redirect_to @project, notice: '项目创建成功。' } format.json { 渲染:显示,状态::创建,位置:@project } 别的 格式.html { 渲染:新 } format.json { 渲染 json:@project.errors,状态::unprocessable_entity } 结尾 结尾 结尾 定义更新 respond_to 做 |格式| 如果@project.update(project_params) format.html { redirect_to @project, notice: '项目已成功更新。' } format.json { 渲染:显示,状态::好的,位置:@project } 别的 format.html { 渲染:编辑 } format.json { 渲染 json:@project.errors,状态::unprocessable_entity } 结尾 结尾 结尾 定义破坏 @project.destroy respond_to 做 |格式| format.html { redirect_to projects_url, notice: '项目已成功销毁。' } format.json { 头 :no_content } 结尾 结尾 私人的 def set_project @project = Project.find(params[:id]) 结尾 def 项目参数 params.require(:project).permit(:name, :date, :location, :integer) 结尾 结尾

front_controller.rb

定义索引 @projects = Project.all 渲染“项目/索引” 结尾 定义显示 结尾 默认配置文件 结尾 结尾

在项目/index.html.erb中

- 链接到“显示”,项目 - 链接到“显示”,front_path(项目)

我已经检查了所有类似的问题。

感谢您的帮助!

Kazei 设计


更新

rake routes | grep front:

           front GET    /front(.:format)             front#index
   front_profile GET    /front/profile(.:format)     front#profile
                 GET    /front/:id(.:format)         projects#show

【问题讨论】:

  • 应该有:constraints => { :user => /[^\/]+/ }
  • @YevgeniyAnfilofyev:感谢您的回答,但就像我说的,我已经尝试过这个解决方案,但它不起作用。
  • 就在您的代码中有 :constraints => { :id => /[^/]+/ } 。也许你拼错了,但仔细检查/[^/]+/ vs /[^\/]+/
  • 我写了这个:get 'front/:id' => 'projects#show', :constraints => { :id => /[^\/]+/ }rake routes 之后,但它不起作用......
  • 您可能需要重新排序您的 routes.rb 文件。在get 'front/:id' => 'projects#show'之前写get 'front/profile' => 'front#profile'

标签: ruby-on-rails ruby routes


【解决方案1】:

你可能想改变你的路线到

get 'front.:id' => 'projects#show'

【讨论】:

  • @RohitJangrid:再次感谢,但它不会改变“。”到 '/' 。但是,如果我自己在 url 中更改它,它就可以工作...
【解决方案2】:

您正在使用一个命名的路由助手,但您没有指定它:

- 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)

documentation - 4.3 Overriding the Named Helpers 查看更多信息

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    相关资源
    最近更新 更多