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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18557977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:38:25  来源:igfitidea点击:

How to fix a missing template error?

ruby-on-railsrubyformsvalidation

提问by user2737114

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.

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

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

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

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

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

回答by Jakob S

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

如果@announcement无法保存 in AnnouncementsController#create,则呈现操作的默认视图 - 在这种情况下create.html.erb不存在。因此,您会看到您看到的错误。

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

通常,创建操作会使用表单重新渲染模板。通常这就是new动作,这意味着添加render "new"else你的create动作部分。

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

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

render "shared/announcements_form"

to your createaction.

到你的create行动。

回答by Agis

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

像在成功保存公告时所做的那样在该块中放置redirect_to或将解决问题。render "shared/announcements_form"else

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

您尝试创建的公告无法保存,因此else块被执行,这就是尝试渲染的内容announcements/create.html.erb。按照惯例,如果操作中没有任何显式render,Rails 会尝试呈现与该操作对应的任何视图。

回答by ashwin tang

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.

检查您的路径,确保您的完整路径拼写正确(即文件夹)而不仅仅是文件名。在文件夹名称中修复了我的错字后,它起作用了。