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?