All threads / Routing for Admin area not working
Ask A Question


You’re not receiving notifications from this thread.

Routing for Admin area not working

Nelson asked in Rails

I've got an application that makes use of an admin area for managing Articles on the frontend.
Its a simple CRUD app where pretty much everything Admin related is namespaced and inside admin folders eg: 'views/admin/...', 'controllers/admin/...', etc

The problem I've got is that the form I implemented for creating and changing Articles is not working at all, it seems to try to use the regular /articles/#post route instead of /admin/articles/#post


<%= form_with model: @article, method: :post do |f| %>
  <div class="form-group">
    <%= f.label :title %>
    <%= f.text_field :title, class: 'form-control input-lg' %>
    <%= f.submit 'Create Article', class: 'btn btn-success' %>
<% end %>


class Admin::ArticlesController < Admin::BaseController
  def new
    @article =

  def create
    @article =
      redirect_to admin_articles_path
      render json: @article

  def update
    @article = Article.find(params[:id])
    if @article.update(article_params)
      redirect_to admin_articles_path
      render json: @article

  def edit
    @article = Article.find(params[:id])

  def destroy
    @article = Article.find(params[:id])
    redirect_to admin_articles_path

  def index
    @page_title = 'Articles'
    @articles = Article.all.order(:created_at)

  def show
    @article = Article.find(params[:id])

  def article_params
    params.require(:article).permit(:title, :excerpt, :content, :category_id, :tags_as_string)


Rails.application.routes.draw do
  devise_for :users

  root 'home#index'

  get 'admin' => 'admin/dashboard#index'
  get 'about' => 'home#about'
  get 'contact' => 'home#contact'

  resources :documents, only: [:index, :show]
  resources :questions, only: [:index]
  resources :categories, only: [:show]
  resources :articles, only: [:index, :show]

  namespace :admin do
    resources :documents
    resources :users
    resources :questions
    resources :categories
    resources :articles

Rake routes (Grep article):

                 articles GET    /articles(.:format)                                                                      articles#index
                  article GET    /articles/:id(.:format)                                                                  articles#show
           admin_articles GET    /admin/articles(.:format)                                                                admin/articles#index
                          POST   /admin/articles(.:format)                                                                admin/articles#create
        new_admin_article GET    /admin/articles/new(.:format)                                                            admin/articles#new
       edit_admin_article GET    /admin/articles/:id/edit(.:format)                                                       admin/articles#edit
            admin_article GET    /admin/articles/:id(.:format)                                                            admin/articles#show
                          PATCH  /admin/articles/:id(.:format)                                                            admin/articles#update
                          PUT    /admin/articles/:id(.:format)                                                            admin/articles#update
                          DELETE /admin/articles/:id(.:format)                                                            admin/articles#destroy

Puma logs:

Started POST "/articles" for at 2019-01-27 06:39:16 -0300

ActionController::RoutingError (No route matches [POST] "/articles"):

Any idea how to force the use of the admin controller?

You are currently using a form_with form helper and passing a model (@article). You can see in the actual source the link this creates. You can also pass an url to form_with, eg form_with url: admin_articles_path, method: :post do |f|. Which should do the "trick" here.

You can just pass in an array of items to tell Rails the correct scoping when it generates the route in the form.

<%= form_with model: [:admin, @article], method: :post do |f| %>

The url option works as well, like eelcoj mentioned, but you'll more commonly see the model being used for forms that are for models. The url option is normally used for custom forms that aren't model backed.

Amazing! Thank yous very much! All working now... Initially It was only Creatint but not saving but I just removed the method: :post

I got this issue this noon and it has been solved by following this thread. Thanks guy.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 70,337+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.