简体   繁体   English

Rails 4-路径中的哈希被错误地渲染

[英]Rails 4 - Hash in route gets rendered wrongly

I have this line of code in routes.rb 我在routes.rb中有这行代码

get 'products#:id' => 'products#index', as: :products_hash

It works, but the problem is that the hash symbol (#) gets rendered as %23. 它可以工作,但是问题是哈希符号(#)呈现为%23。

http://localhost:3000/products%2368

This is what it should be: 这是应该的:

http://localhost:3000/products#68

How can I achieve this? 我该如何实现? Thanks! 谢谢!

Rails 滑轨

I feel you're missing the point of Rails' routing system , which is that you are meant to construct it around objects (hence why you call resources :controller etc). 我觉得您错过了Rails路由系统的要点,那就是您打算围绕对象构建它(因此,为什么要调用resources :controller等)。

Although I don't understand why you're defining the route you are, I'll give you some ideas. 尽管我不明白您为什么要定义路线,但我会给您一些想法。 There is a "wildcard" setting for your routes in Rails, which allows you to define & pass parameters to your application out of the scope of the typical "CRUD" based applications: Rails中的路由有一个“通配符”设置,它使您可以在典型的基于“ CRUD”的应用程序的范围之外定义并向应用程序传递参数:

#config/routes.rb
get 'products#*id' => 'products#index', as: :products_hash

This will give you the ability to send requests to the following route: 这使您能够将请求发送到以下路由:

<%= link_to "Product", products_hash_path("68") %>

You'd then be able to call the param in the controller: 然后,您可以在控制器中调用参数:

#app/controllers/products_controller.rb
class ProductsController < ApplicationController
   def index
      id = params[:id]
   end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM