Jgrimm
Jgrimm

Reputation: 313

Rails no route matches despite rails route output results

Route fails to match despite matching route found in rails routes

No route matches {:action=>"update", :controller=>"croppable_images", :name=>"cover_photo", :record_type=>"Stager"}, possible unmatched constraints: [:name] excluded from capture: No host specified, no public_key specified, no project_id specified
  
ActionController::UrlGenerationError (No route matches {:action=>"update", :controller=>"croppable_images", :name=>"cover_photo", :record_type=>"Stager"}, possible unmatched constraints: [:name]):
  
app/controllers/croppable_images_controller.rb:19:in `edit'
127.0.0.1 - - [04/Feb/2022:16:28:44 CST] "GET /stager/profile/cover_photo HTTP/1.1" 500 143893
http://localhost:3000/stager/profile -> /stager/profile/cover_photo

The route call

@edit_image_url = stagers_stager_croppable_image_path('cover_photo')
<%=
  form_for(
    @croppable_image,
    url: @edit_image_url,
    method: :put
  ) do |f|
%>

routes.rb section:

namespace :stagers, path: 'stager' do
    resource(
      :stager,
      path: 'profile',
      only: %i[edit update],
      path_names: {
        edit: ''
      }
    ) do
      %w[
        profile_photo
        cover_photo
      ].each do |croppable_image_name|
        resources(
          :croppable_image,
          controller: '/croppable_images',
          path: '',
          param: :name,
          only: %i[edit update],
          path_names: {
            edit: ''
          },
          defaults: {
            record_type: 'Stager'
          },
          constraints: {
            name: croppable_image_name
          }
        )
      end
    end
  end

The route:

Helper HTTP Verb Path Controller#Action
edit_stagers_stager_croppable_image_path GET /stager/profile/:name(.:format) croppable_images#edit {:record_type=>"Stager", :name=>"profile_photo"}
stagers_stager_croppable_image_path PATCH /stager/profile/:name(.:format) croppable_images#update {:record_type=>"Stager", :name=>"profile_photo"}
PUT /stager/profile/:name(.:format) croppable_images#update {:record_type=>"Stager", :name=>"profile_photo"}
GET /stager/profile/:name(.:format) croppable_images#edit {:record_type=>"Stager", :name=>"cover_photo"}
PATCH /stager/profile/:name(.:format) croppable_images#update {:record_type=>"Stager", :name=>"cover_photo"}
PUT /stager/profile/:name(.:format) croppable_images#update {:record_type=>"Stager", :name=>"cover_photo"}

Upvotes: 3

Views: 514

Answers (1)

Juan Fuentes
Juan Fuentes

Reputation: 1771

You are setting up that route with a constraint

%w[profile_photo cover_photo].each do |croppable_image_name|
  resrources
    ...
    constraints: { name: croppable_image_name }
  end
end

So you need to add name: to your route, so this should work:

stagers_stager_croppable_image_path(name: 'profile_photo')

However, since you are not using a specific route name on that each loop, calling it with name set to cover_photo will not work. I think you probably want to use a regex like

constraints: { name: /(cover|profile)_photo/ }

Try declaring your routes like this (sorry I messed up your style)

Rails.application.routes.draw do
  namespace :stagers, path: 'stager' do
    resource(:stager, path: 'profile', only: %i[edit update], path_names: { edit: '' }) do
      resources(
        :croppable_image,
        controller: '/croppable_images',
        path: '',
        param: :name,
        only: %i[edit update],
        path_names: {
          edit: ''
        },
        defaults: {
          record_type: 'Stager'
        },
        constraints: {
          name: /(cover|profile)_photo/
        }
      )
    end
  end
end

Upvotes: 2

Related Questions