0

I would like to create routes on RoR for a media website with different sections of articles (ecology, legal, economy, etc...)

I would like my url goes like this

root/magazine/ecology/name-of-articles

(nothing corresponding on rails routing/rails guide, nested and collection routes don't fit for me I think)

here is my try:

get 'magazine/ecology/name-of-article', to: 'articles#name_of_article'
views: folders articles => magazine => ecology => file: name_of_article
controller: articles

But it's not working ...answer from rails below Thx for your help

ActionController::UnknownFormat at /magazine/actualite-juridique/legislation-ce-qui-change-en-2017 ArticlesController#legislation_2017 is missing a template for this request format and variant.

request.formats: ["text/html"] request.variant: []

NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Edouard
  • 3
  • 4
  • do you save the `kind` (ecology, legal, economy, etc...) of article in your `articles` model? – Md. Farhan Memon May 04 '17 at 09:48
  • No my idea was to avoid creating models for each category (belongs to & association). I am trying with collection routes but its working only in second level and not third level. – Edouard May 04 '17 at 10:04
  • Here is my code
     resources :magazine, only: [ :index ] do
        collection do
          get 'actualite-juridique', to: 'magazine#home_actualite_juridique' do
            collection do
              get 'legislation_2017', to: 'magazine#legislation_2017'
            end
          end
        end
      end 
    – Edouard May 04 '17 at 10:06
  • so, you plan to add a new `route` for every new release? – Md. Farhan Memon May 04 '17 at 10:08
  • exactly ! the code I did work on a second level (root/magazine/actualite-juridique) but not on a third level (root/magazine/actualite-juridique/article-on-subject). I m wondering if I can use nested collection routes ? – Edouard May 04 '17 at 10:11
  • Maybe I ve foudn something usefull http://stackoverflow.com/questions/18244453/how-to-nest-collection-routes – Edouard May 04 '17 at 10:14
  • in that case, u will have to add endless number of actions for each article. I don't think that's feasible/recommended. – Md. Farhan Memon May 04 '17 at 10:17
  • I prefer this way (more work but ability to custom) Look in the post above with collections. Tks anyway for you help Farhan Memon – Edouard May 04 '17 at 10:22

1 Answers1

0

Although you don't seek a solution and what you trying to do as discussed in the comments, I don't at all recommend that. I would like to suggest a better method. Take it if you like it.

Run a migration add a column to your articles model which will save the name of section it belongs to. Then, your routes:

get 'magazine/:section/:name' => 'articles#name_of_article' , as: 'sectioned_article'

In your controller:

def name_of_article
  @article = Article.where("section iLike '%#{params[:section]}% AND name iLike '%#{params[:name]}%'").first
end

This way, you can create as many sections/articles you want without code changes. It also gives you customisation option and also reduces your work to great extent which you are ready to do [but now you can utilize that time partying ;)].

Md. Farhan Memon
  • 6,055
  • 2
  • 11
  • 36