Routing for Admin area not working
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
<%= form_with model: @article, method: :post do |f| %> <div class="form-group"> <%= f.label :title %> <%= f.text_field :title, class: 'form-control input-lg' %> </div> <%= f.submit 'Create Article', class: 'btn btn-success' %> <% end %>
class Admin::ArticlesController < Admin::BaseController def new @article = Article.new end def create @article = Article.new(article_params) if @article.save redirect_to admin_articles_path else render json: @article end end def update @article = Article.find(params[:id]) if @article.update(article_params) redirect_to admin_articles_path else render json: @article end end def edit @article = Article.find(params[:id]) end def destroy @article = Article.find(params[:id]) @article.destroy redirect_to admin_articles_path end def index @page_title = 'Articles' @articles = Article.all.order(:created_at) end def show @article = Article.find(params[:id]) end private def article_params params.require(:article).permit(:title, :excerpt, :content, :category_id, :tags_as_string) end end
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 end end
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
Started POST "/articles" for 127.0.0.1 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 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
I got this issue this noon and it has been solved by following this thread. Thanks guy.