trestle icon indicating copy to clipboard operation
trestle copied to clipboard

Routing error with Rails 5 API mode

Open alecrubin opened this issue 6 years ago • 9 comments

Trying to integrate trestle with a rails 5 API app. I added the necessary middlewares, but I keep on getting this error when trying to load /admin. Is there a fix for this?

Showing /Users/alecrubin/.rvm/gems/ruby-2.4.2/gems/trestle-0.8.7/app/views/trestle/resource/index.html.erb where line #4 raised:

No route matches {:action=>"new", :controller=>"items_admin/admin"}

alecrubin avatar Mar 12 '18 01:03 alecrubin

Hi @alecrubin. Have you made any changes to your config/initializers/trestle.rb or admin file, or are they basically pristine from the generator? Anything running like spring that may be interfering with reloading?

Are you able to try creating a fresh Rails app, copy the relevant models across and see if it still occurs?

spohlenz avatar Mar 12 '18 09:03 spohlenz

@alecrubin I run into the same issue, have you found any solution yet ? @spohlenz ?

JulienCorb avatar Apr 04 '18 09:04 JulienCorb

running rails routes gives the following output:

Prefix Verb   URI Pattern                 Controller#Action
     trestle        /admin                      Trestle::Engine
api_v1_posts GET    /api/v1/posts(.:format)     api/v1/posts#index
             POST   /api/v1/posts(.:format)     api/v1/posts#create
 api_v1_post GET    /api/v1/posts/:id(.:format) api/v1/posts#show
             PATCH  /api/v1/posts/:id(.:format) api/v1/posts#update
             PUT    /api/v1/posts/:id(.:format) api/v1/posts#update
             DELETE /api/v1/posts/:id(.:format) api/v1/posts#destroy

Routes for Trestle::Engine:
posts_admin_index GET    /posts(.:format)     posts_admin/admin#index
                  POST   /posts(.:format)     posts_admin/admin#create
      posts_admin GET    /posts/:id(.:format) posts_admin/admin#show
                  PATCH  /posts/:id(.:format) posts_admin/admin#update
                  PUT    /posts/:id(.:format) posts_admin/admin#update
                  DELETE /posts/:id(.:format) posts_admin/admin#destroy
             root GET    /                    trestle/dashboard#index

I just created a fresh app and a Post model with it.

JulienCorb avatar Apr 04 '18 11:04 JulienCorb

@JulienCorb I have been able to replicate this, although I haven't been able to look into it any further than that. I'm also unsure whether it is reasonable to expect an admin engine to run okay within an API-only app -- hopefully it ends up being an easy solve so that it can be supported. However I'm not particularly familiar with the api_only mode in Rails.

spohlenz avatar Apr 04 '18 11:04 spohlenz

@spohlenz Thank you for your answer; I think adding a new route for Trestle::Engine would solve this; But I don't know where I should add this route.

The only problem here I guess is that a rails API-only does not have any new method or route taken into account by default.

JulienCorb avatar Apr 04 '18 12:04 JulienCorb

Are you solve this issue?

euNatan avatar Nov 28 '18 14:11 euNatan

I solved this Issues by adding the missing routes and the middlewares:

  1. add to config/application.rb the following lines inside the class
config.middleware.use ActionDispatch::Flash
config.middleware.use Rack::MethodOverride
config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore
  1. then at the beginning of routes.rb add you routes for you models
Trestle::Engine.routes.draw do
  resources :foos, only: %i[new edit], module: 'foos_admin', controller: 'admin'
end

This issue should go to the wiki

brunitob avatar Mar 08 '19 19:03 brunitob

@brunitob thanks!

nelsonmfinda avatar Jul 21 '21 11:07 nelsonmfinda

Hey! Im having this error and unfortunately @brunitob 's solution isnt fixing it for me. Any more codified solution to using this with rails in the api only? if I comment out config.api_only = true obviously it renders properly

Meryldominguez avatar Dec 18 '23 23:12 Meryldominguez