0

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?

Sender
  • 6,660
  • 12
  • 47
  • 66
Ramonsito
  • 31
  • 1
  • Can you post your `routes.rb` file and `rake routes` output? I don't think I've ever had a route without a prefix. – Jason Nov 09 '15 at 13:56
  • yeah sure, so the routes.rb file includes the following line: resource :global_preferences and here is a screenshot of rake routes result: http://cl.ly/image/0o1e2o0u2Q1i – Ramonsito Nov 11 '15 at 07:07

4 Answers4

1

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.

MkNiz
  • 11
  • 3
0

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.

bkunzi01
  • 4,504
  • 1
  • 18
  • 25
0

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.

Community
  • 1
  • 1
Jason
  • 2,725
  • 2
  • 14
  • 22
0

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

Vasfed
  • 18,013
  • 10
  • 47
  • 53