Ramonsito
Ramonsito

Reputation: 31

how to give names to rails generated routes?

when using the following on routes.rb: resource :my_model

I get a few automatically generated routes. Some of them have a name (just like when manually defining them with 'as' keyword) but some of them don't.. so how can I give them names? Or maybe it's a 'hint' given to me by rails that I'm not supposed to use these routes?

Upvotes: 0

Views: 155

Answers (4)

Vasfed
Vasfed

Reputation: 18504

These routes expect a different request method (this implies additional data in request), and do not need separate name, just use generic global_preferences_path and for example - set method for unobtrusive js in link:

<%= link_to 'Set preference', global_preferences_path(some_parameter:'foo'),
        confirm:'Are you sure?', method: :put %>

or use html form or so

Upvotes: 0

Jason
Jason

Reputation: 2733

Try resources instead of resource. If you want a helper for PATCH, PUT or DELETE, just use the helper for the show action (that you'll get from resources) and specify the HTTP method with method:.

The second answer here has a decent explanation about your resource call.

Upvotes: 0

MkNiz
MkNiz

Reputation: 11

What do you refer to when you say "name", the Prefix when you run rake routes? Many of the HTTP requests (i.e. patch, put, delete) are handled by the controllers and are intended to then either redirect to another path or alter the DOM of the current page if you're using javascript, so they wouldn't have a prefix associated with them as those requests don't have an associated view.

Upvotes: 1

bkunzi01
bkunzi01

Reputation: 4561

When using a singular resource, all CRUD routes will be generated with the exception of an index route. You do not need names for these paths if you are looking to use them as intended. What is your intent for using these routes?

As per the docs:

A singular resourceful route generates these helpers:

new_resourceName_path returns /re/new
edit_geocoder_path returns /geocoder/edit
geocoder_path returns /geocoder

Please post your output via: bundle exec rake routes You'll notice the show and create controller actions share the same path with the only difference being one expects a POST request and the other a GET request. This is some of the magic provided by Rails allowing you to use similarly named routes that map to different actions.

Upvotes: 0

Related Questions