12

I am making a website that has a user page that is supposed to allow users to create announcement posts.

When I try to create a new announcement post through the website, an error pops up as follows:

Template is missing
Missing template announcements/create, application/create with {:locale=>[:en],     :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * ".../app/views"

If I create the create.html.erb at the location, the form will return false as always.

The user page (/app/view/users/show.html.erb):

<% provide(:title, 'Admin Page') %>

<% if signed_in? %>
  <div class="container">
    <%= link_to "Sign out", signout_path, method: "delete" %>
    <br />

     <% if @announcements.any? %>
      <h3>Announcements (<%= @announcements.count %>)</h3>
      <ol>
        <%= render @announcements %>
      </ol>
      <%= will_paginate @announcements %>
    <% end %>

      <%= render 'shared/announcement_form' %>
  </div>

<% else %>
<script type="text/javascript"> window.location.href= '<%= signin_path %>' </script>
<% end %>

shared/_announcement_form.html.erb:

<%= form_for(@announcement) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

   <div class="field">
    <%= f.text_area :title, placeholder: "Compose new title..." %>
  </div>
  <div class="field">
    <%= f.text_area :content, placeholder: "Compose new announcement..." %>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

UsersController:

class UsersController < ApplicationController
  def new
  end

  def show
    @user = User.find(params[:id])
    @announcement = current_user.announcements.build if signed_in?
    @announcements = @user.announcements.paginate(:page => params[:a_page], :per_page  => 10)
  end
end

AnnouncementsController:

class AnnouncementsController < ApplicationController
  before_action :signed_in_user, only: [:create, :destroy]

  def create
    @announcement = current_user.announcements.build(announcement_params)

    if @announcement.save
      flash[:success] = "Announcement created!"
      redirect_to root_url
    else
      flash[:error] = "Failed to create announcement!"
    end
  end

  def destroy
  end

  private

  def announcement_params
    params.require(:announcement).permit(:content)
    params.require(:announcement).permit(:title)
  end
end

I have checked that I can create the announcement manually in the console. What am I doing wrong?

Mischa
  • 42,876
  • 8
  • 99
  • 111
user2737114
  • 125
  • 1
  • 2
  • 6

4 Answers4

11

If @announcement fails to save in AnnouncementsController#create, you render the default view for the action - in this case create.html.erb which doesn't exist. Thus, you get the error you're seeing.

Usually, the create action would re-render the template with the form. Usually that's the new action, which would mean adding render "new" to the else part of your create action.

In your case however, it seems like it's in the "shared/announcements_form" partial, which would mean adding

render "shared/announcements_form"

to your create action.

Jakob S
  • 19,575
  • 3
  • 40
  • 38
  • Thanks, although putting render "shared/_announcements_form" still create the same "Template error", but i get the idea. I add "redirect_to :back" and it works better, as _announcements_form.html.erb is a partial of users/show.html.erb, and somehow render or redirect_to "users/show" would cause another error. Also, the reason of announcement failed to save, is that announcement_params should be in one line, params.require(:announcement).permit(:title, :content), not separated into two. – user2737114 Sep 01 '13 at 22:16
1

Putting a redirect_to or render "shared/announcements_form" in that else block like you're doing when the announcement is successfully saved will solve the problem.

The announcement you're trying to create fails to save, so the else block is executed and this is what tries to render announcements/create.html.erb. By convention, if there isn't any explicit render in an action, Rails tries to renders whatever view corresponds to that action.

Agis
  • 32,639
  • 3
  • 73
  • 81
0

Check your path, make sure your full path is spelled correctly (i.e folders) not just the file name. After fixing my typo in the folder name, it worked.

0

Posting this here in case someone else has the same issue.

I kept having the error ActionView::MissingTemplate using Rails 7, Windows 11 and Git Bash. Starting the Rails server with Git Bash threw the Missing Template bug all the time, as if Rails was ignoring my files. However, launching the server with the Windows Command Prompt worked perfectly fine, as stated here.

I stumbled upon this link which provided me the answer. One of the directories in my root path was capitalized as follows: path/to/FOO.

Renaming my folder to foo did not work straight away. I had to rename it first to a different name such as blablabla then back to foo. Thus the full path is path/to/foo. This finally allowed me to start the server with Git Bash.

I later found this StackOverflow question which also deals with this problem. I am adding it here for additional documentation.

Gabriel Guérin
  • 430
  • 2
  • 13