paul
paul

Reputation: 1242

Rails Routing: Remove the edit suffix to url

I am creating a website with a blog module. A blog post can either be a draft or published.

A published post can no longer be edited, and a draft cannot be viewed (only edit)

I currently have a resource defined as

resources :posts, :path => "blog" do
  collection do
    get 'drafts'
  end
end

I can access the drafts list using blog/drafts, creating new ones posts using blog/new, and editing drafts through blog/:id/edit.

However, I'd like new drafts to be created using blog/drafts/new and edited using blog/drafts/:id

I need to define the new, create, edit and update routes to use this new scheme. The new and create routes seem quite simple. However I do not know how to handle the edit route in order to remove the action name part.

Also, while looking at the default routes definition, I found in actionpack-3.2.9/lib/action_dispatch/routing/mapper.rb the following :

member do
  get    :edit if parent_resource.actions.include?(:edit)
  get    :show if parent_resource.actions.include?(:show)
  [...]
end

I do not understand how rails differentiates the :edit and the :show routes, and map the urls accordingly.

Thanks

Upvotes: 0

Views: 610

Answers (1)

Ahmad Sherif
Ahmad Sherif

Reputation: 6223

You can use the following routes. Keep in mind that it requires different file hierarchy, rake routes should be your friend in this.

namespace :blog do
  resources :drafts, :controller => :posts, only: [:new, :edit]
  resources :posts, only: :show
end

Upvotes: 2

Related Questions