0

I'm attempting to create some urls for a model that I want display. I have articles, which belong to sections which belong to issues.

I would like my URLs for the article show action to look like this:

/issue-slug/section-slug/article-slug issues articles and sections have slugs that are stored in the db.

Right now I have a backend section called 'pressroom' and I have the following routes for that. Here is the whole routes.rb file

MaskmagazineCom::Application.routes.draw do

 devise_for :users, :path_names => { :sign_up => "register"}, :controllers => {    :registrations => "registrations" }
 # The priority is based upon order of creation: first created -> highest priority.
 # See how all your routes lay out with "rake routes".

 root 'magazine#index'
 get 'users/' => 'users#index'


 # Lobby Routes

 # /log-in

 devise_scope :user do
 get '/sign-in' => 'devise/sessions#new'
end

 # /subscribe

 get 'subscribe' => 'subscribe#stepone'
 get 'subscribe/sliding-scale' => 'subscribe#steptwo'
 get 'subscribe/sliding-scale/subscriber' => 'subscribe#subscriber'
 get 'subscribe/sliding-scale/supporter' => 'subscribe#supporter'
 get 'subscribe/sliding-scale/sustainer' => 'subscribe#sustainer'
 post 'subscribe/sliding-scale/:type' => 'subscribe#createSubscription'



# Pressroom Routes

get '/pressroom' => 'pressroom#index'
scope 'pressroom' do
resources :issues, :articles, :sections, :users, :authors
end

How can I pull out the show action and route it to the url that I described?

EDITED:

I've come up with what i want it to do in the routes file, but i need the corresponding controller code:

get '/:issue_slug/:section_slug/:article_slug' => 'article#show'

1 Answers1

0

I'd recommend checking out friendly_id for the slugs.

for the routes, you'll want your routes to read:

# Mag Routes

get '/mag' => 'mag#index' #or wherever you're headed
  scope 'mag' do
    resources :issues do
      resources :sections do
        resources :articles
      end
    end
  end
end
Dudo
  • 4,002
  • 8
  • 32
  • 57
  • I'm already using friendly_id for the slugs, i'm confused at what i need to put in the article controller's show action to get the url i want. –  Jan 08 '14 at 21:38
  • ah. that chunk of resources I just showed you, that goes in `config/routes.rb`. can you post your entire routes file for me? – Dudo Jan 08 '14 at 21:45
  • Its above, i've added in your routes –  Jan 08 '14 at 21:56
  • That's not quite right, i want the article#show to display at /mag/issue_slug/section_slug/article_slug. The rest of the routes are fine –  Jan 08 '14 at 22:05
  • well, mag is actually `/pressroom` the way you have it laid out now. but that's how you get the path to show the way you want. when you link to an article then, you link it with `[@issue,@section,@article]` to get to your article#show action. example - http://www.ecorebox.com/boxes/MM-1/uin/ERB0002512 - where is `mag` in your routes? – Dudo Jan 08 '14 at 22:10
  • `get '/mag' => 'article#show' scope 'mag' do resources :issues do resources :sections do resources :articles end end end` Adding that doesn't make the article show link correct. It does however force the issue show to 'mag/issues/issue_slug' –  Jan 08 '14 at 22:20
  • right, you can't tell /mag to go straight to article#show. You're telling rails, "when going to `site.com/mag` to render the article controller, and the show action". I don't think that's going to do what you want. you want the nested resources to show up under the page that your going to... updated answer again. Without knowing 100% of your code, is this making sense? – Dudo Jan 08 '14 at 22:31
  • this still isn't working right, i have a feeling something is the matter with how sections are linked to the issue and article models. –  Jan 09 '14 at 02:25