Reputation: 679
I'm having some issues with a set of views in my rails app. I created ran scaffold to create a posts model, controller and views. They work just fine on their own, but they refuse to pull in the application.html.erb layout.
This is the posts controller:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render action: 'show', status: :created, location: @post }
else
format.html { render action: 'new' }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :body, :datecreated, :datemodified, :published, :author)
end
end
Here is one of the views I want to use the application layout:
<%= render 'form' %>
Here is the application.html.erb:
<!DOCTYPE html>
<html>
<head>
<title>Coreyonrails</title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
I saw a post on here regarding the subclass for a controller having < ActionController::Base My controller did not have that, and still does not so that didn't help.
Hopefully this isn't a dumb question, I'm still new to rails.
Edited to show file structure.
app
controllers
application_controller.rb
pages_controller.rb
posts_controller.rb
views
layouts
_header.html.erb
application.html.erb
pages
about.html.erb
contact.html.erb
home.html.erb
posts
_form.html.erb
_posts.html.erb
edit.html.erb
index.html.erb
new.html.erb
show.html.erb
The rest of the rails generated folders are there as well, I just showed what is relevant to the issue.
The ApplicationController does inherit from ActionController::Base, this is the entire file:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
end
The pages controller looks similar, and those pages are pulling the application.html.erb correctly without any special work needed.
Here is the repo on github, https://github.com/CoreyT355/website1. Maybe that'll help more.
Upvotes: 2
Views: 5070
Reputation: 4668
In my case, only one controller's views were not rendering with layouts/application.html.erb
. Turns out the controller in question was inheriting from ActionController::Base
instead of ApplicationController
.
Incorrect:
MyController < ActionController::Base
Correct:
MyController < ApplicationController
Upvotes: 2
Reputation: 76774
Several things (should be a comment, but I'll write it here for clarity):
- Check you have
app/views/layouts/application.html.erb
- Check you have
app/controllers/application_controller.rb
inheriting fromActionController::Base
When you mention the use of <%= render "form" %>
, what do you mean? Where is this rendered? If it's rendered from an action, it will be shown on your application layout automatically
Can you post more code for us, specifically posts_controller
, application_controller
, routes
and application
layout file?
Upvotes: 3