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.
application.html.erblocated in the directory structure? Is it happening for all controllers, or just one, or just one action in a controller (if so which action?)application.html.erb? It should beapp/views/layouts/application.html.erbApplicationControllershould inherit fromActionController::Base. It should look like this:ApplicationController < ActionController::Baseapplication_html.erb? Was that a typo in updating the question? Rails normally wants the file to be namedapplication.html.erb