2

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.

6
  • where is application.html.erb located in the directory structure? Is it happening for all controllers, or just one, or just one action in a controller (if so which action?) Commented May 2, 2014 at 1:25
  • 1
    what's the file structure of your application.html.erb? It should be app/views/layouts/application.html.erb Commented May 2, 2014 at 2:24
  • your application ApplicationController should inherit from ActionController::Base . It should look like this: ApplicationController < ActionController::Base Commented May 2, 2014 at 2:27
  • edited the post with more info from the comments here Commented May 2, 2014 at 13:29
  • Is that file supposed to be named application_html.erb? Was that a typo in updating the question? Rails normally wants the file to be named application.html.erb Commented May 2, 2014 at 13:32

2 Answers 2

3

Several things (should be a comment, but I'll write it here for clarity):

  1. Check you have app/views/layouts/application.html.erb
  2. Check you have app/controllers/application_controller.rb inheriting from ActionController::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?

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.