Rails 使用 2 个或更多项(Get 和 Post)路由 REST 客户 URL
Rails routing REST with 2 or more items (Get and Post) custome URL
我有一个 rails 应用程序想要修复。我是 rails 的新手,不知道如何正确执行此操作。有一个数据库、模型和 table 有很多产品。我为用户创建了一种方法,可以从包含数据库条目的下拉列表中选择 select 2 个产品。用户 select 的 2 个产品然后可以比较它们,我设置了一个视图来显示 2 个产品的规格。
我确定我没有遵循 REST 并且没有正确执行此操作,尽管它正在运行。在 Rails 中使用 CRUD 操作允许显示和索引,它还会生成一个 URL,其中包含一个这样的产品的产品 ID,我没有这样做过。相反,我将一个实例变量 @product 传递给 productpicker controller/view 表单,并使用返回的参数来查找要在比较 controller/view.
中显示的数据库项目
我想完成的事情:
- 一个 URL 看起来像 www.domain.com/oneproduct-vs-someotherprodct
- 一次获取 2 个产品并显示它们的正确 RESTfull 方式
- 一个包含所有链接的站点地图,以便我可以将它们提供给 google 用于 SEO 目的。我假设链接是动态的,没有人会知道我有一些产品与其他产品,因此站点地图和上面 url 中的文本一样必不可少。
- 必要时适当的变量白名单
请帮助指导我正确的方向。非常感谢大家,感谢您帮助菜鸟。
到目前为止我做了什么:
路线:
get 'products/pickproducts'
post 'products/pickproducts', to: 'products#compareproducts'
这里可以添加 slug 吗?
这是我的做法。
config/routes.rb
resources :products do # sets up the usual index, new, create, show, edit, update, destroy
get :compare, on: :collection
end
您将能够在 www.example.com/products/compare
访问此路径,并且当您有两个产品 ID 要传入时,您可以将它们附加到 URL: www.example.com/products/compare?product_id1=345&product_id2=432
。
(请注意,这是一个 GET
请求;POST
通常按照约定保留给 创建记录的请求 - 和 PUT
或 PATCH
用于 更新 记录的请求。GET
请求通常也是唯一需要地址本身参数的请求。)
app/controllers/products_controller.rb
class ProductsController < ApplicationController
# ...
def compare
@products = Product.all
@product1 = Product.find(params[:product_id1]) rescue nil
@product2 = Product.find(params[:product_id2]) rescue nil
end
# ...
end
app/views/products/compare.html.erb
(免责声明:我之前没有使用表单创建 GET
请求,但我 认为 这应该有效:)
<%= form_tag compare_products_path, method: :get do |f| %>
<%= select_tag :product_id1, options_from_collection_for_select(@products, 'id', 'name') %>
<%= select_tag :product_id2, options_from_collection_for_select(@products, 'id', 'name') %>
<%= f.submit %>
<% end %>
<% if @product1 && @product2 %>
<%= @product1.id %>
<%= @product1.brand %>
<%= @product1.modname %>
<hr>
<%= @product2.id %>
<%= @product2.brand %>
<%= @product2.modname %>
<% else %>
Please select two products above to compare.
<% end %>
我不确定的一部分是 SEO 和网络爬虫 - 我没有特别深入地研究这些,所以不能提供真正的建议。您也许可以将其设置为您的路线:
get ':product_id1-vs-:product_id2' => 'products#compare'
... 然后,如果有效,URL 将是 www.example.com/products/-vs-
(是的,我知道这不太好)和 www.example.com/products/345-vs-432
。但是,compare_products_path
现在需要两个参数,我不确定您如何将所选产品放入表单指向的 URL。
我有一个 rails 应用程序想要修复。我是 rails 的新手,不知道如何正确执行此操作。有一个数据库、模型和 table 有很多产品。我为用户创建了一种方法,可以从包含数据库条目的下拉列表中选择 select 2 个产品。用户 select 的 2 个产品然后可以比较它们,我设置了一个视图来显示 2 个产品的规格。
我确定我没有遵循 REST 并且没有正确执行此操作,尽管它正在运行。在 Rails 中使用 CRUD 操作允许显示和索引,它还会生成一个 URL,其中包含一个这样的产品的产品 ID,我没有这样做过。相反,我将一个实例变量 @product 传递给 productpicker controller/view 表单,并使用返回的参数来查找要在比较 controller/view.
中显示的数据库项目我想完成的事情:
- 一个 URL 看起来像 www.domain.com/oneproduct-vs-someotherprodct
- 一次获取 2 个产品并显示它们的正确 RESTfull 方式
- 一个包含所有链接的站点地图,以便我可以将它们提供给 google 用于 SEO 目的。我假设链接是动态的,没有人会知道我有一些产品与其他产品,因此站点地图和上面 url 中的文本一样必不可少。
- 必要时适当的变量白名单
请帮助指导我正确的方向。非常感谢大家,感谢您帮助菜鸟。
到目前为止我做了什么:
路线:
get 'products/pickproducts'
post 'products/pickproducts', to: 'products#compareproducts'
这里可以添加 slug 吗?
这是我的做法。
config/routes.rb
resources :products do # sets up the usual index, new, create, show, edit, update, destroy
get :compare, on: :collection
end
您将能够在 www.example.com/products/compare
访问此路径,并且当您有两个产品 ID 要传入时,您可以将它们附加到 URL: www.example.com/products/compare?product_id1=345&product_id2=432
。
(请注意,这是一个 GET
请求;POST
通常按照约定保留给 创建记录的请求 - 和 PUT
或 PATCH
用于 更新 记录的请求。GET
请求通常也是唯一需要地址本身参数的请求。)
app/controllers/products_controller.rb
class ProductsController < ApplicationController
# ...
def compare
@products = Product.all
@product1 = Product.find(params[:product_id1]) rescue nil
@product2 = Product.find(params[:product_id2]) rescue nil
end
# ...
end
app/views/products/compare.html.erb
(免责声明:我之前没有使用表单创建 GET
请求,但我 认为 这应该有效:)
<%= form_tag compare_products_path, method: :get do |f| %>
<%= select_tag :product_id1, options_from_collection_for_select(@products, 'id', 'name') %>
<%= select_tag :product_id2, options_from_collection_for_select(@products, 'id', 'name') %>
<%= f.submit %>
<% end %>
<% if @product1 && @product2 %>
<%= @product1.id %>
<%= @product1.brand %>
<%= @product1.modname %>
<hr>
<%= @product2.id %>
<%= @product2.brand %>
<%= @product2.modname %>
<% else %>
Please select two products above to compare.
<% end %>
我不确定的一部分是 SEO 和网络爬虫 - 我没有特别深入地研究这些,所以不能提供真正的建议。您也许可以将其设置为您的路线:
get ':product_id1-vs-:product_id2' => 'products#compare'
... 然后,如果有效,URL 将是 www.example.com/products/-vs-
(是的,我知道这不太好)和 www.example.com/products/345-vs-432
。但是,compare_products_path
现在需要两个参数,我不确定您如何将所选产品放入表单指向的 URL。