Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.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 如何在routes中设置自己的参数?_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 如何在routes中设置自己的参数?

Ruby on rails 如何在routes中设置自己的参数?,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我在我的文件中运行一个脚本,该脚本从数据库生成内容,并根据从中提取的内容通过javascript显示出来,如下所示 但是,当我键入localhost/findme/search word时,我只会让它返回一个,当我检查rails服务器时,它会显示LIMIT 1,我如何将它们全部提取出来,然后遍历它们 它似乎只拉第一个 在我的路线上,我有 match'/findme/:plant_site'=>“定位#显示” Route.find(params[:plant_site]) 将返回找到的第一个

我在我的文件中运行一个脚本,该脚本从数据库生成内容,并根据从中提取的内容通过javascript显示出来,如下所示

但是,当我键入localhost/findme/search word时,我只会让它返回一个,当我检查rails服务器时,它会显示LIMIT 1,我如何将它们全部提取出来,然后遍历它们

它似乎只拉第一个

在我的路线上,我有

match'/findme/:plant_site'=>“定位#显示”

Route.find(params[:plant_site]) 
将返回找到的第一个项目。试试这个

# /app/controllers/the_controller.rb

def the_action
  @routes = Route.where(id: params[:plant_site]).all
end


# /app/views/the_controller/the_action.html.erb

<% @routes.each do |route| %>
  <%= ... %>
<% end %>
#/app/controllers/the_controller.rb
定义_操作
@路线=路线。其中(id:params[:plant_site])。所有
结束
#/app/views/the_controller/the_action.html.erb
编辑:

虽然
id
列通常是主键列,因此上面的结果仍然只返回1个。尝试
Route.where(plant\u site:params[:plant\u site])
可能。我猜这个专栏是
:plant\u site

编辑:


添加了文件名,以指示代码应该从beck03076的注释中转到何处。

您如何指定id:?因为问题中的
Route.find(…)
将通过
id
查找,所以我假设该列为
id
。只需将
id:
更改为正确的列名即可。我还为我的答案添加了更多的信息。哦,好的,效果很好,谢谢。我会尽快给你一个检查,所以路线。在plant:site列中,哪里可以找到参数为:plant\u site的所有内容?是的,
where
方法将返回一个与查询匹配的数组,
find
将返回第一个结果,其中
id
与第一个参数匹配。@beck03076如何将其放入控制器,然后在JS中使用?