简体   繁体   English

Rails用2个或更多项目(获取和发布)自定义URL路由REST

[英]Rails routing REST with 2 or more items (Get and Post) custome URL

I have a rails app that I would like to fix up. 我有一个要修复的Rails应用程序。 I am new to rails and do not know how to do this correctly. 我是Rails的新手,不知道如何正确执行此操作。 There is a database, model, and table that has many products. 有一个包含许多产品的数据库,模型和表。 I created a way for a user to select 2 of those products from a dropdown that contains database entries. 我为用户提供了一种从包含数据库条目的下拉菜单中选择其中2种产品的方法。 The user selects 2 products and then can compare them, I set up a view to display the specs to of the 2 products. 用户选择2个产品,然后可以进行比较,我设置了一个视图来显示2个产品的规格。

I am sure I am not following REST and not doing this right though it is working. 我确定我没有遵循REST,尽管可以正常工作,但是没有正确执行此操作。 Using the CRUD operations in Rails allows show and index, it also makes a URL that has the product id for one such product, I have not done it like this. 在Rails中使用CRUD操作可以显示和编制索引,它还可以使URL具有此类产品的产品ID,但我还没有这样做。 Instead I pass an instance variable @product to productpicker controller/view form and use the params returned to look up database items to display in the comparison controller/view. 相反,我将实例变量@product传递给productpicker控制器/视图表单,并使用返回的参数来查找要在比较控制器/视图中显示的数据库项。

What I would like to accomplish: 我要完成的工作:

  1. a URL that looks like www.domain.com/oneproduct-vs-someotherprodct 看起来像www.domain.com/oneproduct-vs-someotherprodct的URL
  2. a correct RESTfull way to fetch 2 products at once and show them 一次正确提取两种产品并显示它们的正确RESTfull方法
  3. a sitemap that has all the links so I can provide them to google for SEO purposes. 具有所有链接的站点地图,因此我可以将它们提供给Google以进行SEO。 I assume the links will be dynamic and no will know that I have someproduct-vs-someotherproduct, so a site map is essential as is the text in the url above. 我假设链接是动态的,并且不会知道我有someproduct-vs-someotherproduct,因此站点地图和上面url中的文本一样必不可少。
  4. Proper white-listing of variables where necessary 必要时将变量正确列入白名单

Please help guide me in the right direction. 请帮助指导我正确的方向。 Much appreicated guys, thanks for helping out a noob. 非常感谢的人,感谢您帮助菜鸟。

What I have done so far: 到目前为止,我所做的是:

Routes: 路线:

get 'products/pickproducts'

post 'products/pickproducts', to: 'products#compareproducts'

Is it possible to add slugs here ? 可以在这里添加吗?

Here's the way I'd do it. 这就是我要做的方式。

config/routes.rb config / routes.rb

resources :products do # sets up the usual index, new, create, show, edit, update, destroy
  get :compare, on: :collection
end

You will be able to access this path at www.example.com/products/compare , and when you have two product IDs to pass in, you can just append them to the URL: www.example.com/products/compare?product_id1=345&product_id2=432 . 您将可以在www.example.com/products/compare上访问此路径,并且当您要传递两个产品ID时,只需将它们附加到URL: www.example.com/products/compare?product_id1=345&product_id2=432

(Note that this is a GET request; POST is usually reserved by convention for requests that create records - and PUT or PATCH for requests that update records. GET requests are usually the only ones that expect parameters in the address itself, too.) (请注意,这是一个GET请求;按照惯例, POST通常保留给创建记录的请求 -而PUTPATCH保留给更新记录的请求GET请求通常也是唯一希望在地址本身中输入参数的请求。)

app/controllers/products_controller.rb 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 app / views / products / compare.html.erb

(Disclaimer: I haven't used a form to create a GET request before, but I think this should work:) (免责声明:我之前没有使用过表单来创建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 %>

The one part I'm not sure about is the SEO and webcrawlers - I haven't got into these particularly heavily so can't really advise. 我不确定的一部分是SEO和网络抓取工具-我还没有深入研究这些内容,因此无法真正建议。 You might be able to set this up as your route instead: 也许可以将其设置为您的路线:

get ':product_id1-vs-:product_id2' => 'products#compare'

... and then, if it works, the URLs would be www.example.com/products/-vs- (yeah, I know that's not great) and www.example.com/products/345-vs-432 . ...,然后,如果可行,URL将会是www.example.com/products/-vs- (是的,我知道那不是很好)和www.example.com/products/345-vs-432 However, compare_products_path will now require two arguments, and I'm not sure how you'd get your selected products into the URL that the form points at. 但是, compare_products_path现在将需要两个参数,而且我不确定如何将所选产品放入表单所指向的URL中。

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

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