简体   繁体   English

如何在link_to中添加:id以实现Rails?

[英]How to add :id in link_to for rails?

My goal is to create a way for sellers (User) to upload files to buyers (Buyer) in their, the User's, Sales page (their sales history page). 我的目标是为卖方(用户)在其“用户”的“销售”页面(其销售历史页面)中将文件上传到买方(“买方”)提供一种方法。 I am using Devise for user roles btw. 我正在将Devise用于用户角色。

I have links, for example, I have the URL " localhost:3000/orders/28/sales_uploads/new " working so I can upload to the specific sale. 我有链接,例如,我的URL“ localhost:3000 / orders / 28 / sales_uploads / new ”可以正常工作,因此我可以上传到特定的销售商品。

Though, within the Sales page for the User/seller, I want them to be able to upload the file to the buyer, (buyers buy product, seller then uploads file after purchase - each product/purchase will be custom), either through the sales page, preferably, or by clicking a link - which is where I'm having issues. 虽然,我希望他们能够在用户/卖方的“销售”页面上将文件上传到买方(买方购买产品,卖方然后在购买后上传文件-每个产品/购买都是自定义的),无论是通过销售页面,最好还是单击链接-这就是我遇到的问题。 I want the seller to click a link "Upload" which then directs them to the page to upload the file for the order. 我希望卖方单击链接“上传”,然后将其定向到页面以上传订单文件。

In the Sales page view, (views/sales_uploads/sales), I have 在“销售”页面视图((视图/ sales_uploads / sales))中,我有

 <%= link_to "Upload", new_order_sales_upload_path(@order, order) %>

but I get an error that there is no order_id. 但我收到一个错误,即没有order_id。

How can I make it so the link links to the order with the order ID. 我该怎么做,以便链接链接到带有订单ID的订单。

I have a 我有一个

 <% @orders.each do |order| %>

so it cycles through and I can't figure out why it won't work. 所以它循环,我不知道为什么它不起作用。

Here is my github https://github.com/brandnamewater/digitalcommerce 这是我的github https://github.com/brandnamewater/digitalcommerce

As you'll see in the view mentioned above, I have the cycling working. 正如您在上述视图中所看到的,我正在骑自行车。 I'm assuming I'm having an issue elsewhere, possibly in the controller. 我假设我在其他地方有问题,可能是在控制器中。 Though, I simply cannot figure it out. 不过,我根本无法弄清楚。

Just in case, it's better off i link the entire app as there may be other issues I'm not aware of that are causing this. 以防万一,最好是我链接整个应用程序,因为可能还有其他我不知道的问题导致了这个问题。

I have only been coding regularly for a few weeks now and I'm sure it's something very basic to some of you but I can't figure it out. 我现在只是定期编码几个星期,我敢肯定这对你们中的一些人来说是非常基础的,但是我无法弄清楚。 I have been attempting this for 2 nights now and no other Google searches, Stack searches, etc. have helped. 我已经尝试了2个晚上,但是其他Google搜索,堆栈搜索等都没有帮助。

Well, I had the order mixed up 好吧,我把订单弄混了

It was : 它是 :

<%= link_to "Upload", new_order_sales_upload_path(@order, order) %>

and should had been: 并且应该是:

 <%= link_to "Upload", new_order_sales_upload_path(order ,@order) %>

The correct code you're looking for is: 您要查找的正确代码是:

<% @orders.each do |order| %> <%= link_to "Upload", new_order_sales_upload_path(order) %> <% end %>

If you run bin/rails routes | grep new_order_sales_upload 如果您运行bin/rails routes | grep new_order_sales_upload bin/rails routes | grep new_order_sales_upload in your terminal it will provide some output which shows us the route helper method and also any parameters we need to pass into the route helper. bin/rails routes | grep new_order_sales_upload在您的终端中,它将提供一些输出,向我们显示路线帮助器方法以及需要传递到路线帮助器中的所有参数。

For your case it will output 对于您的情况,它将输出

new_order_sales_upload GET    /orders/:order_id/sales_uploads/new(.:format)                                            sales_uploads#new

This shows us the route helper is called new_order_sales_upload and that it expects a single parameter :order_id . 这向我们显示了路由帮助程序,称为new_order_sales_upload ,它需要一个参数:order_id

If you were to output @order in your view like <%= @order.inspect %> you would find it is nil because this instance variable is not being set. 如果要在视图中输出@order类的<%= @order.inspect %>您会发现它为nil因为未设置此实例变量。

Your loop @orders.each is setting a variable called order . 您的循环@orders.each设置了一个名为order的变量。

The reason your answer <%= link_to "Upload", new_order_sales_upload_path(order, @order) %> works is because the route helper is only using the first variable order and the second one @order is nil and not being used. 您的答案<%= link_to "Upload", new_order_sales_upload_path(order, @order) %>起作用的原因是,路由助手仅使用第一个变量order ,而第二个@ordernil而不被使用。

You can read a lot more about routing in rails from the official guide https://guides.rubyonrails.org/routing.html 您可以从官方指南https://guides.rubyonrails.org/routing.html阅读有关Rails中布线的更多信息。

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

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