【问题标题】:Missing Template - Template is Missing缺少模板 - 缺少模板
【发布时间】:2017-12-20 13:17:39
【问题描述】:

首先我要承认我对 Rails 很陌生。尽管我过去在 Rails 方面有过成功的记录,但我遇到了一个无法解决的错误。这是以下错误:

缺少带有 {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html 的模板 /ctoneposts , :builder, :ruby, :coffee, :jbuilder]}。

奇怪的是,在我的代码停止工作之前,我能够使用 form_for 创建帖子。我有两个帖子可以在我的网络应用程序中动态添加。在此之后,我添加了 devise gem 并开始将用户分配到帖子的过程(在我进行测试之前一切都很好!)

这是我的控制器:

    class CtonepostsController < ApplicationController
  def index
      @ctoneposts = Ctonepost.all
      @ctoneposts = Ctonepost.order(created_at: :desc)
  end

  def create
      post_params = params.require(:ctonepost).permit(:content)

      @ctonepost = Ctonepost.new(content: post_params[:content])

      if @ctonepost.save
          redirect_to ctoneposts_path
      else 
          render ctoneposts_path
      end
  end 

end

我收到有关渲染 ctoneposts_path 的错误。

这是我的 index.html.erb 文件:

<%= render "ctoneform" %>
<br>

<%= render @ctoneposts %>

_ctonepost.html.erb:

<div class="col-xs-8">
    <p><%= ctonepost.content %></p>
    <small><%= ctonepost.created_at.strftime("%b %d, %Y") %></small>
</div>

我的观点是片面的。我做了一些改变(我不记得我做了什么),它最终吐出了一个部分视图错误。我什至在更改文件保存的内容等方面走了这么远,但似乎没有任何效果,它是快把我逼疯了!请帮助新的 Ruby on Rails 编码器!

【问题讨论】:

    标签: ruby-on-rails post devise


    【解决方案1】:

    我可以在这里看到一些东西。首先,您没有render 路径。你渲染一个模板。所以调用render 然后调用路径是行不通的。

    如果你想回到索引或任何其他路径,你可以

    redirect_to some_path
    

    或者您可能只需要再次呈现“新”模板(可能包括表单):

    render :new
    

    【讨论】:

    • 我最终希望它重定向到“ctoneposts_path”,因为动态表单在该页面上,并且帖子出现在该页面上。如果我执行“else redirect_to ctoneposts_path”,则不会出现错误,但帖子不会显示。显然这是没有意义的,因为它是“if”语句。
    • 然后尝试渲染 :index。
    • :index 在 Ctoneposts#create 中创建 ArgumentError。我可能会放弃应用程序并重新开始,而不需要部分视图。我并不总是这样做,所以我很陌生。
    • 感谢您的帮助。为了以后的发展,我会记住你所说的。我很感激!
    【解决方案2】:

    由于您在可以使用的模板中渲染部分内容,

    <%= render partial: 'ctoneposts/ctonepost' %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-09
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-18
      相关资源
      最近更新 更多