Routes with Dash `-` Instead of Underscore `_` in Ruby on Rails

前端 未结 4 1064
别那么骄傲
别那么骄傲 2020-12-04 18:33

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

4条回答
  •  臣服心动
    2020-12-04 19:12

    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'.

提交回复
热议问题