2

Update #3 ... fixed! Solution was moving the new name space code (get 'dash', to: 'dashes#show') to the bottom of the routes.rb file just above the root "campaigns#index" entry.

Update #2 ... it's not a pluralization as it's called singularly, shown below & removing the named route automatically corrects all issues.

Update #1 ... found reason why no one had listed period in place of slash in URL ... it's unix nouns, not english ... IE: if you search for 'dot' in place of '.', then you get all sorts of answers.

I'm flumoxed by this one ... made my first named route the other day, everything looked great until suddenly it appears that the edit page when called from the named route doesn't update properly, with error ...

No route matches [PATCH] "/dash.6"

Removal of the named route takes me back to normal routes & all options working. I can't find a mention of routing which uses '.' instead of '\', so I'm lost. My route file below & then the route results from rails server ...

Rails.application.routes.draw do

  devise_for :users, controllers: { sessions: 'users/registrations' }

  # map.login '/login', :controller => 'sessions', :action => 'new'  ## 3rd try
  # get '/dash', :controller => 'dashes', :action => 'show'  ## 2nd try
  # get 'dash', to: 'dashes#show'  ## Original named route

  resources :dashes
  resources :campaigns
  resources :players
  resources :countries

  root "campaigns#index" 

  # yank later
  resources :neighborhoods
end

Rails results on server ...

Paths Matching (dashes):
dashes_path     GET     /dashes(.:format)   

dashes#index
    POST    /dashes(.:format)   

dashes#create
Paths Containing (dashes):
dashes_path     GET     /dashes(.:format)   

dashes#index
    POST    /dashes(.:format)   

dashes#create
new_dash_path   GET     /dashes/new(.:format)   

dashes#new
edit_dash_path  GET     /dashes/:id/edit(.:format)  

dashes#edit
    GET     /dashes/:id(.:format)   

dashes#show
    PATCH   /dashes/:id(.:format)   

dashes#update
    PUT     /dashes/:id(.:format)   

dashes#update
    DELETE  /dashes/:id(.:format)   

dashes#destroy

My form is standard rails generated & works whenever I remove the named route ... edit.html.haml renders the _form partial ...

%h1 Editing dash

= render 'form'

= link_to 'Show', @dash
\|
= link_to 'Back', dashes_path

_form.html.haml

  = simple_form_for(@dash) do |f|
  = f.error_notification

  .form-inputs
    = f.input :name
    = f.association :user
    = f.association :dashcampaigns
    = f.association :dashplayers

  .form-actions
    = f.button :submit
Mirv - Matt
  • 553
  • 7
  • 22
  • can you post your form? You have an issue defining the method (I suppose that's the problem) – rogelio Mar 13 '17 at 22:28
  • @rogelio ...done, ty on fast reply, I'm still trying stuff and not getting much - might have to call it quits for the night in the next 15 minutes. – Mirv - Matt Mar 13 '17 at 22:36
  • 1
    What do you mean that it works whenever you remove the named route? Does the form you pasted above not generate a problem? Typically the issue with a dot is when you use plural named-route by mistake instead of singular. See here: http://stackoverflow.com/questions/16264981/rails-dot-instead-of-slash-in-url – AmitA Mar 13 '17 at 22:39
  • So, in my routes.rb file...if I comment out the line with the named route...my dash#edit works again. – Mirv - Matt Mar 13 '17 at 22:40
  • The http://stackoverflow.com/questions/16264981/rails-dot-instead-of-slash-in-url rails solution didn't work for me, even restarted server :( – Mirv - Matt Mar 13 '17 at 22:43
  • What are you trying to achieve by adding a named routes? Can't you use one of the 7 RESTful routes you get for free when you do `resources :dashes`? The `simple_form_for` form builder assumes that you're following the RESTful pattern, so if you muck with the typical 7 RESTful routes, you have to override the `url:` option manually, and potentially the `method:` option. – AmitA Mar 13 '17 at 22:43
  • The fact your `rake routes` have an entry that looks like `dashes#show PATCH /dashes/:id(.:format) ` is alarming. `dashes#show` should be a GET, not a PATCH. Can you think of a reason why you might have it? – AmitA Mar 13 '17 at 22:44
  • @AmitA I'm not sure why it's going to patch either...all code comes from the rails guides. I really just want an alias to the 7 restful routes just to know how to do it & it's a neat whistle. If there's a better way please tell me! In the end I want, `domain.com/dash` to route to ` resources :dashes` – Mirv - Matt Mar 13 '17 at 22:47
  • doing `resources :dashes` and `simple_form_for(@dash) do |f|` should be enough – rogelio Mar 13 '17 at 22:49
  • when you say to route `domain.com/dash` to `resources :dashes`, do you mean for one particular action or all actions? I.e., do you want instead of having `domain.com/dashes/1` going to `DashesController#show` with `id: 1` you want to have `domain.com/dash/1` do the same, and instead of `domain.com/dashes/1/edit` use `domain.com/dash/1/edit`, etc.? – AmitA Mar 13 '17 at 22:49
  • @AmitA ... I might be looking at this wrong, but what I thought was going to happen is the `:dashes` would function normally, then `/dash` would just be an extra alias for showing the current user their dash. So yes, I still want `/dashes/1/edit` to work. – Mirv - Matt Mar 13 '17 at 22:54
  • I have to head out for the night - got a meetup - will check back in next 24 hrs – Mirv - Matt Mar 13 '17 at 22:55
  • The current user's dashes should be under `domain.com/dashes`, not `domain.com/dash`. Going to `domain.com/dashes` takes you to the index action, which is the action that is the one you're looking for (show a list of dashes). Take a look at this table for the routes that you get for free: http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions. – AmitA Mar 13 '17 at 22:56
  • The form builder, upon receiving a `@dash` that is already persisted (`@dash.new_record?` returns false) invokes the builder as if you said `url: dash_path(@dash), method: :patch`. This allows the form to wire to the `#update` action. If suddenly dash_path means something else, you're in trouble. – AmitA Mar 13 '17 at 23:00
  • One more thing - if you really want an alias, can you try changing your alias line from `get 'dash', to: 'dashes#show'` to `get 'dash/:id', to: 'dashes#show'`? I think I might have misread your question and what you're looking to achieve. Maybe this suggestion is what you were after. – AmitA Mar 13 '17 at 23:01
  • @Brad Werth, I don't think this question is duplicate. Pending OP's response to my answer below, I suspect his question will require editing to clarify that it is not about misuse of the Rails URL helper methods like in the duplicate question you linked to, but rather mucking with `routes.rb` inappropriately. This is a different use case - note how the OP does not alter the calling concern `simple_form_for` like the alleged duplicate's OP does when explicitly using `projects_path(1)`. – AmitA Mar 13 '17 at 23:41
  • @coreyward please see objection for dup marking above. – AmitA Mar 13 '17 at 23:52
  • I'm still pouring over these, but I found it at the meetup with a friend ... it turns out that moving the ` get 'dash', to: 'dashes#show' ` to bottom of the routes file & just above the `root` entry – Mirv - Matt Mar 14 '17 at 01:29

1 Answers1

6

As described in this question, typically the issue is when you confuse between collection (plural) and member (singular) controller actions.

A collection controller action is an action that does not have an ID since it does not manipulate an existing resource (dash) or since it works on a group of resources. The RESTful collection actions are: index, new, and create. Each of them has a helper method and a "verb" (method):

  • index: url: dashes_path, method: :get (method get is default)
  • create: url: dashes_path, method: :post
  • new: url: new_dash_path, method: :get

Note how both index and create share the same plural URL helper method dashes_path. Only the method option differentiates between them.

A member controller action is an action that has an ID of the resource it is manipulating (one particular dash). The RESTful collection actions are:

  • edit: url: edit_dash_path(@dash), method: :get
  • show: url: dash_path(@dash), method: :get
  • update: url: dash_path(@dash), method: :patch
  • destroy: url: dash_path(@dash), method: :destroy

See how except edit, all other actions use the same singular URL helper method dash_path(@dash).

You can find further details on this in the guides.

Now, the "dot instead of slash" symptom is when you mistakenly try to point to a member action in a way that should be used for collection actions. So if you try to do dashes_path(@dash) - there is no such thing. The only parameter dashes_path accepts is format, which is added to the URL in the end after a dot, that's why you're seeing a weird URL such as /dash.6.

Rails form builder form_for and extensions such as simple_form_for need to decide whether to point the form action at the #create collection action (when rendering the #new collection action) or #update member action (when rendering the #edit member action). They do so "magically" by taking a look at the object you give them when you do simple_form_for(@dash). If @dash.new_object? is true, they point the form action to #create, if it's false they point it to #update.

In your case when you used it "out of the box" it was all good. It started acting up on you when you added this line to your routes.rb:

get 'dash', to: 'dashes#show'

By default show is a member action, not a collection action. It must receive an ID. I think that this is why Simple Form is acting up on you. Instead of that alias, try this one:

get 'dash/:id', to: 'dashes#show'

Let us know if this fixes the issue.

And in general, I recommend to "work with" the RESTful routing rather then "working against" them. It is very rare to need to add named routes in routes.rb. There are exceptions, but I don't think it is justified in your case to deviate from conventions and try to use dash/1 rather than dashes/1. "Working with" Rails will get you the productivity boost it is known for, not "working against" it by trying to force it for relatively small details like this one.

Community
  • 1
  • 1
AmitA
  • 3,239
  • 1
  • 22
  • 31
  • I'm confused - this is all rails recommendations, which part is against rails? A short recommendation about ordering of the routes & It would have been a 30 second feature add. Edit: Link to specific part ... http://guides.rubyonrails.org/routing.html#naming-routes – Mirv - Matt Mar 14 '17 at 14:51
  • Accepted the answer - it wasn't the fix but you put the most work in there. See my last comment of question for the fix. – Mirv - Matt Sep 05 '18 at 18:53