Rails issue with possible unmatched constraints in routes

Viewed 76

I have an issue and I don't understand how can I solve it.

I have a model with subclass inheritance...

class Content < ApplicationRecord
   TYPE = {
      article: {name: 'Reportage', class: 'Reportage'},
      news: {name: 'News', class: 'News'},
      guide: {name: 'Guida', class: 'Guide'},
      review: {name: 'Recensione', class: 'Review'},
      gallery: {name: 'Gallery', class: 'Gallery'},
      video: {name: 'Video', class: 'Video'}
    }
end

In db I have a type column to store the subclass

class Review < Content

end

My get route is

  get ':content_type/:content_slug(/:content_page_position_index/:content_page_slug)', to: 'contents#show', constraints: { content_type: /#{ContentsController::TYPES_FOR_ROUTING.values.map { |v| Regexp.escape v }.join('|')}/ }, as: :content

result: "reportage|news|guida|recensione|gallery|video"

And inside the controller I have

TYPES_FOR_ROUTING = Content::TYPE.map{ |k,v| [v[:class],v[:name].downcase] }.to_h

result: {"Reportage"=>"reportage", "News"=>"news", "Guide"=>"guida", "Review"=>"recensione", "Gallery"=>"gallery", "Video"=>"video"}

For video, gallery and news I don't have any issue because the italian name is the same of the english name that is the class name. But for review I have an issue.

ActionView::Template::Error (No route matches {:action=>"show", :content_slug=>"porsche-panamera-hybrid-2021-la-prova-su-strada-in-provenza", :content_type=>"review", :controller=>"contents"}, possible unmatched constraints: [:content_type])

Content Type is "review", the class name is review because I want to keep the english language. But in the routes it looks for "recensione". I need for SEO to have the italian language.

How can I pass to the route to match costraints, instead of the type attribute (review), the name. I can use Content::TYPE, but I don't know how to pass it.

0 Answers
Related