'Rails not rendering template
I have a Ruby on Rails application that was originally set up for Rails API only, however, I need to set up a permalink page through Rails model-view-controller.
I defined a route
get 'posts/:id', :to => 'posts#display'
In posts controller
def display
render "show"
end
In /views/posts/show.html.erb
<p>Hello</p>
When I hit the URL in browser: localhost:3000/posts/1 I would expect to just see the "Hello" rendered from the view
However, I'm just getting a blank text.
From the Rails server log
Started GET "/posts/1" for ::1 at 2022-01-28 18:08:19 -0500
Processing by PostsController#display as HTML
Parameters: {"id"=>"1"}
Completed 200 OK in 2ms (Views: 0.1ms | ActiveRecord: 0.0ms | Allocations: 451)
I'm not sure if the assets are not compiling?
Versions:
ruby 2.7.2
rails 6.1.3
Solution 1:[1]
You are most likely missing the layout that yields the views, also as per my comment on your question, rename the method to show, change the route to match and remove the render statement from the controller action.
You have not posted nearly enough of your code to provide a full solution but your controller can specify which layout to use with the layout command or if descending from application controller then make sure application_controller.rb is specifying which layout to use and check the yield statement in that layout template.
Create the layout if you don't have one You seem to be missing Rails standard conventions and if you fight them you'll loose
route should become
resources :posts, only: [:show]
your controller should be either
class PostsController < ApplicationController
def show
end
end
or
class PostsController < ApplicationController
layout "posts"
def show
end
end
and you need to check the application_layout.html.erb in the views/layouts folder and ensure that it has a yield statement in the body or if you use the second controller option that defines a different layout then you will need to create that layout and again ensure that after the head section you have a body and you call yield in that. Ensure that you use all the correct headers in the head section
a typical layout looks something like this
<!DOCTYPE html>
<html>
<head>
<title>Css menu</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag "application", media: "all" %>
</head>
<body>
<%= yield %>
</body>
</html>
If you do not have the correct <!DOCTYPE html> and <html> declarations your browser won't know what to do
if you are providing a specific posts_layout.html.erb in the views/layouts folder to handle the specific layout declared in my second posts controller example you will need to just bear in mind that any other controllers needing to render views will need to specify which layout to use so I would stick to the application_layout.html.erb and make sure that it is being used in the application_controller.rb
Rails will automatically render the template named after the action and format so no need to call render in the controllers action.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
