在这种情况下如何使用Rails helper link_to RESTfully?

时间:2021-09-14 20:50:14

I basically want to use link_to to link to the index method of a controller. I tried:

我基本上想用link_to链接到控制器的索引方法。我试过了:

<%= link_to 'Recipes', Recipe %> 

but that outputs:

但那个输出:

<a href="/recipes/Recipe">Recipes</a>

Which is clearly not right, if it left off that last bit it would do exactly what I want it to. I thought that with RESTful stuff I somehow would start to leave out the action or something like that. What am I misunderstanding?

这显然是不对的,如果它停止了最后一点它会完全按照我的意愿行事。我认为,使用RESTful的东西,我会以某种方式开始忽略动作或类似的东西。我有什么误会?

1 个解决方案

#1


6  

With the restful routes, the majority of the time you're expected to call a helper method to generate the route.

对于宁静的路由,大多数时候您需要调用辅助方法来生成路由。

eg:

link_to 'Recipes', recipes_path

There is an optimization where you can just pass in a recipe object, and it will call the helper method for you behind the scenes: eg:

有一个优化,您只需传入一个配方对象,它将在幕后为您调用辅助方法:例如:

link_to 'Recipe X', @recipe

is the same as

是相同的

link_to 'Recipe X', recipe_path(@recipe)

However, it's just a special case.

但是,这只是一个特例。

What you are doing is passing The recipe class itself, not a valid recipe object. As rails doesn't know to handle this, as a fallback it just calls .to_s on whatever you've given it, and then gives that to recipe_path, which is why you see the strange URL.

你正在做的是传递配方类本身,而不是有效的配方对象。因为rails不知道要处理这个问题,所以作为一个后备,它只会在你给它的任何内容上调用.to_s,然后将它提供给recipe_path,这就是你看到奇怪URL的原因。

Tip: Use the _path helper methods rather than the _url methods.

_url gives you a full URL such as http://*.com/recipes/5 whereas _path just gives you /recipes/5.
The problem with the full URL is that a lot of the time in production your rails app is running as a mongrel sitting behind a load balancer, so it thinks it's host name is actually 1.2.3.4 (or whatever the internal LAN IP is) rather than the real URL, and so will serve broken links.

_url为您提供完整的URL,例如http://*.com/recipes/5,而_path只提供/ recipes / 5。完整URL的问题在于,生产rails应用程序的大部分时间都是作为坐在负载均衡器后面的杂种运行,因此它认为它的主机名实际上是1.2.3.4(或者内部LAN IP)而不是真正的URL,因此将提供断开的链接。

#1


6  

With the restful routes, the majority of the time you're expected to call a helper method to generate the route.

对于宁静的路由,大多数时候您需要调用辅助方法来生成路由。

eg:

link_to 'Recipes', recipes_path

There is an optimization where you can just pass in a recipe object, and it will call the helper method for you behind the scenes: eg:

有一个优化,您只需传入一个配方对象,它将在幕后为您调用辅助方法:例如:

link_to 'Recipe X', @recipe

is the same as

是相同的

link_to 'Recipe X', recipe_path(@recipe)

However, it's just a special case.

但是,这只是一个特例。

What you are doing is passing The recipe class itself, not a valid recipe object. As rails doesn't know to handle this, as a fallback it just calls .to_s on whatever you've given it, and then gives that to recipe_path, which is why you see the strange URL.

你正在做的是传递配方类本身,而不是有效的配方对象。因为rails不知道要处理这个问题,所以作为一个后备,它只会在你给它的任何内容上调用.to_s,然后将它提供给recipe_path,这就是你看到奇怪URL的原因。

Tip: Use the _path helper methods rather than the _url methods.

_url gives you a full URL such as http://*.com/recipes/5 whereas _path just gives you /recipes/5.
The problem with the full URL is that a lot of the time in production your rails app is running as a mongrel sitting behind a load balancer, so it thinks it's host name is actually 1.2.3.4 (or whatever the internal LAN IP is) rather than the real URL, and so will serve broken links.

_url为您提供完整的URL,例如http://*.com/recipes/5,而_path只提供/ recipes / 5。完整URL的问题在于,生产rails应用程序的大部分时间都是作为坐在负载均衡器后面的杂种运行,因此它认为它的主机名实际上是1.2.3.4(或者内部LAN IP)而不是真正的URL,因此将提供断开的链接。