Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 4-路由中的哈希错误呈现_Ruby On Rails_Ruby_Ruby On Rails 4_Routes - Fatal编程技术网

Ruby on rails Rails 4-路由中的哈希错误呈现

Ruby on rails Rails 4-路由中的哈希错误呈现,ruby-on-rails,ruby,ruby-on-rails-4,routes,Ruby On Rails,Ruby,Ruby On Rails 4,Routes,我在routes.rb中有这行代码 get 'products#:id' => 'products#index', as: :products_hash 它可以工作,但问题是哈希符号(#)被渲染为%23 http://localhost:3000/products%2368 应该是这样的: http://localhost:3000/products#68 我怎样才能做到这一点?谢谢 导轨 我觉得您忽略了的要点,即您应该围绕对象构建它(因此,您需要调用resources:contro

我在routes.rb中有这行代码

get 'products#:id' => 'products#index', as: :products_hash
它可以工作,但问题是哈希符号(#)被渲染为%23

http://localhost:3000/products%2368
应该是这样的:

http://localhost:3000/products#68
我怎样才能做到这一点?谢谢

导轨

我觉得您忽略了的要点,即您应该围绕对象构建它(因此,您需要调用
resources:controller
等)

虽然我不明白你为什么要定义你的路线,但我会给你一些想法。Rails中有一个路由设置,允许您在典型的基于“CRUD”的应用程序范围之外定义参数并将参数传递给应用程序:

#config/routes.rb
get 'products#*id' => 'products#index', as: :products_hash
这将使您能够向以下路由发送请求:

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

然后可以在控制器中调用参数:

#app/controllers/products_controller.rb
class ProductsController < ApplicationController
   def index
      id = params[:id]
   end
end
#app/controllers/products_controller.rb
类ProductsController<应用程序控制器
def索引
id=params[:id]
结束
结束

你读过这个吗?是的,但这对hahaI没有什么帮助。为什么要使用
http://localhost:3000/products#68
而不是
http://localhost:3000/products/68
?我认为您应该在指向帮助程序的
链接的
锚定
键中手动指定。在routes中指定它可能是不可能的,因为routes主要用于解析和选择路径,而#后面的部分不是路径的一部分。我所说的“不可能”是指您需要修改rails的方法以使其工作。也许可以写一个助手。你不想这样-浏览器根本不会将#之后的内容发送到服务器