在 Ruby on Rails 中使用破折号“-”而不是下划线“_”的路由

发布于 2024-10-24 00:59:23 字数 419 浏览 2 评论 0原文

我希望我的网址使用破折号 - 而不是下划线 _ 作为单词分隔符。例如 controller/my-action 而不是 controller/my_action

我对两件事感到惊讶:

  1. 谷歌等人。继续区分它们。
  2. Ruby on Rails 没有简单的全局配置参数来将路由中的 - 映射到 _。或者确实如此?

我最好的解决方案是使用 :as 或命名路由。

我的想法是修改 Rails 路由以检查全局配置,并在分派到控制器操作之前将 - 更改为 _

有更好的办法吗?

I want my urls to use dash - instead of underscore _ as word separators. For example controller/my-action instead of controller/my_action.

I'm surprised about two things:

  1. Google et al. continue to distinguish them.
  2. That Ruby on Rails doesn't have a simple, global configuration parameter to map - to _ in the routing. Or does it?

The best solution I've is to use :as or a named route.

My idea is to modify the Rails routing to check for that global config and change - to _ before dispatching to a controller action.

Is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浪菊怪哟 2024-10-31 00:59:23

对于 Rails 3 及更高版本,您可以这样做:

resources :user_bundles, :path => '/user-bundles'

另一个选择是通过初始化程序修改 Rails。
但我不推荐这样做,因为它可能会在未来的版本中崩溃(编辑:在 Rails 5 中不起作用)。

如上所示使用 :path 效果更好。

# Using private APIs is not recommended and may break in future Rails versions.
# https://github.com/rails/rails/blob/4-1-stable/actionpack/lib/action_dispatch/routing/mapper.rb#L1012
#
# config/initializers/adjust-route-paths.rb
module ActionDispatch
  module Routing
    class Mapper
      module Resources
        class Resource
          def path
            @path.dasherize
          end
        end
      end
    end
  end
end

With Rails 3 and later you can do like this:

resources :user_bundles, :path => '/user-bundles'

Another option is to modify Rails, via an initializer.
I don't recommend this though, since it may break in future versions (edit: doesn't work in Rails 5).

Using :path as shown above is better.

# Using private APIs is not recommended and may break in future Rails versions.
# https://github.com/rails/rails/blob/4-1-stable/actionpack/lib/action_dispatch/routing/mapper.rb#L1012
#
# config/initializers/adjust-route-paths.rb
module ActionDispatch
  module Routing
    class Mapper
      module Resources
        class Resource
          def path
            @path.dasherize
          end
        end
      end
    end
  end
end
画骨成沙 2024-10-31 00:59:23

您可以重载控制器和操作名称以使用破折号:

# config/routes.rb
resources :my_resources, path: 'my-resources' do
  collection do
    get 'my-method', to: :my_method
  end
end

您可以在控制台中测试:

rails routes -g my_resources
my_method_my_resources GET  /my-resources/my-method(.:format) my_resources#my_method

You can overload controller and action names to use dashes:

# config/routes.rb
resources :my_resources, path: 'my-resources' do
  collection do
    get 'my-method', to: :my_method
  end
end

You can test in console:

rails routes -g my_resources
my_method_my_resources GET  /my-resources/my-method(.:format) my_resources#my_method
后eg是否自 2024-10-31 00:59:23

您可以使用命名路由。它将允许使用“-”作为单词分隔符。在routes.rb中,

map.name_of_route     'a-b-c',       :controller => 'my_controller', :action => "my_action"

现在像http://my_application/abc这样的url将转到指定的控制器和操作。

另外,在本例中创建动态 url

map.name_of_route    'id1-:id2-:id3',       :controller => 'my_controller', :action => "my_action"

'id1, id2 & id2 将作为 http 参数传递给操作

在您的操作和视图中,

name_of_route_url(:id1=>val1, :id2=>val2, :id3=>val3) 

将评估为 url 'http://my_application/ val1-val2-val3'。

You can use named routes. It will allow using '-' as word seperators. In routes.rb,

map.name_of_route     'a-b-c',       :controller => 'my_controller', :action => "my_action"

Now urls like http://my_application/a-b-c would go to specified controller and action.

Also, for creating dynamic urls

map.name_of_route    'id1-:id2-:id3',       :controller => 'my_controller', :action => "my_action"

in this case 'id1, id2 & id2 would be passed as http params to the action

In you actions and views,

name_of_route_url(:id1=>val1, :id2=>val2, :id3=>val3) 

would evaluate to url 'http://my_application/val1-val2-val3'.

新人笑 2024-10-31 00:59:23

如果你在控制器和视图文件中使用下划线,那么只需在你的路由文件中使用破折号,它就会工作..

get 'blog/example-text' 这是我的这个控制器的路由

def example_text
end <-- 这是我的控制器

, example_text.html.erb 是文​​件

,这是实际链接 site.com/blog/example-text

我认为这对我有用,并且它比下划线 SEO 更有效

if you use underscores in a controller and view file then just use dashes in your routes file, and it will work..

get 'blog/example-text' this is my route for this controller

def example_text
end <-- this is my controller

and example_text.html.erb is the file

and this is the actual link site.com/blog/example-text

i figured this is works for me, and it's more effective than underscores SEO wise

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文