【问题标题】:Rails routes invisible prefixRails 路由不可见前缀
【发布时间】:2020-02-20 23:32:17
【问题描述】:

嗨,我在 rails 路线中的前缀是不可见的。

这是我的文件 routes.rb

Rails.application.routes.draw do

  get '/gossip/:id', to: 'gossip#show'
  get '/welcome/:user_entry', to: 'welcome#personnal'
  root 'home#show'
  get '/team', to: 'team#show'
  get '/contact', to: 'contact#show'
 end

当我在终端运行 rails 路线时,我看到了:

Prefix Verb URI Pattern                                                                              Controller#Action
                          GET  /gossip/:id(.:format)                                                                    gossip#show
                          GET  /welcome/:user_entry(.:format)                                                           welcome#personnal
                     root GET  /                                                                                        home#show
                     team GET  /team(.:format)                                                                          team#show
                  contact GET  /contact(.:format)                                                                       contact#show
       rails_service_blob GET  /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
rails_blob_representation GET  /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
       rails_disk_service GET  /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
update_rails_disk_service PUT  /rails/active_storage/disk/:encoded_token(.:format)                                      active_storage/disk#update
     rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format)                                           active_storage/direct_uploads#create

如果我在id 之前删除:,我会看到前缀,但我的路线不再是动态的。有小费吗?谢谢

【问题讨论】:

  • 你的前缀不可见是什么意思?哪条线与您的预期不同?你到底期待什么?
  • 我需要前缀才能在<%= link_to "click here", prefix_path %> 中使用它?
  • 你只去showgossip吗? newcreate等呢?
  • 目前我在数据库中只有八卦,我在一个页面中显示所有标题,我希望每个八卦的内容都有一个页面。

标签: ruby-on-rails ruby routes prefix invisible


【解决方案1】:

您可以通过添加as: :some_route_name来命名路由:

get '/gossip/:id', to: 'gossip#show', as: :gossip

或使用:

resources :gossip, only: [:show]

【讨论】:

  • 我尝试使用<%= link_to "click here", gossip_path(@id) %>,但我无法使用它,我也不太了解如何制作动态链接
  • @FFouquet42 尝试使用resources,同时确保@id 不是nil
  • 在哪里使用 resources 在 shell 中?在代码中?我不知道它是什么以及它是如何工作的
  • @FFouquet42 在路线中(见更新的答案),更多在guides.rubyonrails.org/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-25
  • 2016-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-10
  • 2011-06-09
相关资源
最近更新 更多