AnApprentice
AnApprentice

Reputation: 110950

Rails, link_to is using a period and not a / in the URL?

<%= link_to 'View the item', items_url(@item, :only_path => false) %>

Which is generating:

http://0.0.0.0:3000/items.124/

This is no good as it has a period where it should have a forward slash like:

http://0.0.0.0:3000/items/124/

Anyone know y? thanks

Upvotes: 9

Views: 1786

Answers (3)

trpubz
trpubz

Reputation: 27

using Rails 7.0.5 at time of response. @Damith is correct, the path helper needs to be singular and not plural.

This response is meant to illuminate a skosh. The plurality in Rails helper paths point to page indexes, or collection of objects, whereas the single object reference in the path helper, identifies it as a reference for a single object, typically used in conjunction with a #show action in a controller. When you used the plural version items_url with more objects than expected, Rails treats the additional argument as a format specifier because the route definition is not expecting an ID for a nested resource.

Using the singular version item_url correctly indicates to Rails that you're looking for a nested route that includes an ID attribute on your model, hence fixing the malformed URI. It's a subtle but important distinction in Rails path helpers.

Upvotes: 0

AnApprentice
AnApprentice

Reputation: 110950

shoot, should be item_url not items_url

Upvotes: 15

Walking Wiki
Walking Wiki

Reputation: 699

I had a similar problem with my form_for generated URL. I had accidentally put resource my_resource instead of resources my_resources.

Upvotes: 0

Related Questions