gpt4 book ai didi

ruby-on-rails - 如何修复丢失的模板错误?

转载 作者:数据小太阳 更新时间:2023-10-29 06:53:27 25 4
gpt4 key购买 nike

我正在制作一个网站,该网站的用户页面应该允许用户创建公告帖子。

当我尝试通过网站创建新的公告帖子时,弹出如下错误:

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"

如果我在该位置创建 create.html.erb,表单将一如既往地返回 false。

用户页面(/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 %>

共享/_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 %>

用户 Controller :

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

公告 Controller :

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

我已经检查过我可以在控制台中手动创建公告。我做错了什么?

最佳答案

如果 @announcement 未能在 AnnouncementsController#create 中保存,您将呈现该操作的默认 View - 在本例中为 create.html.erb 不存在。因此,您会收到所看到的错误。

通常,创建操作会重新呈现带有表单的模板。通常这是 new 操作,这意味着将 render "new" 添加到 create 操作的 else 部分.

然而,在您的情况下,它似乎在 "shared/announcements_form" 部分中,这意味着添加

render "shared/announcements_form"

到您的创建操作。

关于ruby-on-rails - 如何修复丢失的模板错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18557977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com