With a rails-api Rails 5 app work with the standard installation procedure?
The current version isn't updated for Rails 5 yet. There's currently a PR open (https://github.com/thoughtbot/administrate/pull/376) for this. I'm using this fork right now with rails 5 and it's working fine.
As for rails-api, I'm not sure if the setup is different.
@pauldavis, @nburkley I had to add gem 'rails-api', require: false and add require 'rails-api' to ApplicationController, otherwise each update/delete form was rendered as POST.
I added the gem from @hayesr's branch:
gem 'administrate', github: 'hayesr/administrate', branch: 'rails5_compat'
Unfortunately this yields an ActionView::Template::Error error:
{"status":500,"error":"Internal Server Error","exception":"#\u003cActionView::Template::Error: undefined method new_admin_booking_path'
Full stack trace: https://gist.github.com/richardvenneman/a454922c9874bcf46a3c40a880716b1d
@richardvenneman How can I reproduce? Is this just a straight new rails-api app with "booking" model?
@hayesr yes, I setup a sample test repository which reproduces the issue:
https://github.com/richardvenneman/rails-5-administrate
Thanks for having a look!
Thanks, I'll check into it as soon as I get some time.
With Rails 5 API mode, a REST-ful resources only creates routes for pure CRUD actions. This means no new or edit routes. That's where the error is coming from.
I'm not sure how to properly fix this, but setting config.api_only = false in application.rb solves the issue (but that almost defeats the purpose of API mode). Once you've done that, you'll get a params issue from Kaminari. Apparently 0.16 doesn't want to work with Rails 5
To solve that, I forked @hayesr's repo and removed the reference to Kaminari from the gemspec. Then, I just made sure there was a working version of Kaminari before Administrate in my project's Gemfile.
gem 'kaminari', github: 'amatsuda/kaminari', branch: 'master'
gem 'administrate', github: 'kieraneglin/administrate', branch: 'master'
Not a super solution, but it works! Kaminari should be pushing 0.17 soon which will solve this issue, but this works in the interim
@richardvenneman Here is my setup for Rails API only apps using @hayesr rails5_compat branch and inspired by https://rrott.com/blog/ror/rails-5-api-with-activeadmin-integration.html
gem 'administrate', github: 'hayesr/administrate', branch: 'rails5_compat'rails generate administrate:install namespace :admin, as: '' do
# resources :accounts
get '/accounts', to: 'accounts#index', as: 'admin_accounts'
get '/accounts/new', to: 'accounts#new', as: 'new_admin_account'
post '/accounts', to: 'accounts#create', as: ''
get '/accounts/:id', to: 'accounts#show', as: 'admin_account'
get '/accounts/:id/edit', to: 'accounts#edit', as: 'edit_admin_account'
patch '/accounts/:id', to: 'accounts#update'
delete '/accounts/:id', to: 'accounts#destroy'
root to: "accounts#index"
end
Finally, as per the suggestion by https://rrott.com/blog/ror/rails-5-api-with-activeadmin-integration.html Update config/application.rb file to include necessary middleware for View Templates: ActionDispatch::Flash, Rack::MethodOverride and ActionDispatch::Cookies
Example:
module NewApiApp
class Application < Rails::Application
# ...
config.middleware.use ActionDispatch::Flash
config.middleware.use Rack::MethodOverride
config.middleware.use ActionDispatch::Cookies
#to fix CSRF protection error configure the default session store - cookie_store
config.middleware.use ActionDispatch::Session::CookieStore
end
end
I think this is pretty much all the steps I took. I hope it helps.
Please let me know if you face any issue over this method or if you think it has a drawback.
I very much like administrate, thanks for the hard work :). Im excited to help contribute working through issues regarding Rails 5 API support.
@bhtabor solution worked for me as well, though I dont like losing the simplicity and versatility of the resources :resource_name DSL for Rails routes. Any chance there would be better support for this in the future?
Also, as for adding the middleware back into the application config, is there a way Administrate could create a controller concern for itself that adds these things if the application is a Rails 5 API?
Note that with the latest solution above, and the latest version of administrate (0.3.0), only the changes to the routes and the middleware are needed. The @hayeser gem is not needed (at least, I am looking at my first UserDashboard on screen for my Rails 5 (API) app, and I did not need to use that gem). I did also need to add a bourbon fix though.
@stratigos is right-- the official 0.3.0 version of Administrate works on a Rails 5 API application with just the route and middleware changes as well as a Bourbon fix. No need for the custom fork anymore it seems.
I outlined those steps to get it up and running in a recent blog post for any who need them.
I'm going to close this as I just installed Administrate on a Rails 5.1 API. We don't use the apps ApplicationController. Instead, we have our own. Since we use generators and whatnot, you may want to do this:
# config/application.rb
config.api_only = false
This way Administrate can render flashes and such.
@BenMorganIO FYI- if you don't want to disable api_only entirely, you can add the missing middleware for Administrate using these instructions. Hope that helps!
Most helpful comment
@richardvenneman Here is my setup for Rails API only apps using @hayesr rails5_compat branch and inspired by https://rrott.com/blog/ror/rails-5-api-with-activeadmin-integration.html
gem 'administrate', github: 'hayesr/administrate', branch: 'rails5_compat'rails generate administrate:installFinally, as per the suggestion by https://rrott.com/blog/ror/rails-5-api-with-activeadmin-integration.html Update config/application.rb file to include necessary middleware for View Templates: ActionDispatch::Flash, Rack::MethodOverride and ActionDispatch::Cookies
Example:
I think this is pretty much all the steps I took. I hope it helps.
Please let me know if you face any issue over this method or if you think it has a drawback.