I generated an API-only rails app with Rails 5 via rails new <application-name> --api
. I've decided I want to include a view for testing some things and am having issues getting a view to load.
I created a users/index.html.erb file with some text and my controller is now simply def index; end
def index; end
but there is nothing appearing when I hit the /users URL. I also tried commenting out the # config.api_only = true
in config/application.rb but that didn't affect anything. Any suggestions on how to proceed?
You don't need to uncomment config.api_only = true
for this purpose, just inherit your controller from ActionController::Base
, or do it in your ApplicationController
(default for common rails generation).
Code:
YourController < ActionController::Base
ApplicationController < ActionController::Base
this is from the ActionController::Metal docs https://apidock.com/rails/ActionController/Metal
it says:
ActionController::Metal by default provides no utilities for rendering >views, partials, or other responses aside from explicitly calling of >response_body=, content_type=, and status=. To add the render helpers >you're used to having in a normal controller, you can do the following:
class HelloController < ActionController::Metal include AbstractController::Rendering include ActionView::Layouts append_view_path "#{Rails.root}/app/views" def index render "hello/index" end end
So I've tried it myself and adding just by adding the two modules actually work just fine when using it for ActionController::API
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.