简体   繁体   English

我在新的Rails应用程序中创建了一个文件,当我运行http:// localhost页面时,我收到一条Missing Template错误消息

[英]I created a file in my new rails app and when I run the http://localhost page I get a Missing Template error message

This is the error I'm getting: 这是我得到的错误:

ActionView::MissingTemplate in Posts#index Posts#index中的ActionView :: MissingTemplate

Missing partial text_posts/_text_post with {:locale=>[:en], :formats=>[:html],... 缺少带有{:locale => [:en] 、: formats => [:html],...的部分text_posts / _text_post ...

Extracted source (around line #5): 提取的源(第5行附近):

    3 </div>
    4
    5 <%= render @posts %>

Here's the code in the file app/views/posts/index.html.erb 这是文件app / views / posts / index.html.erb中的代码

    <div class="page-header">
    <h1>Home</h1>
    </div>
    <%= render @posts %>

I'm following along the 'Rails Crash Course' book and this is one of the steps to create a social network. 我遵循的是“ Rails Crash Course”这本书,这是创建社交网络的步骤之一。 I don't know the reason for the error. 我不知道错误的原因。

As you're using <%= render @posts %> , i'm am sure this will call the partial file with leading _ symbol on same directory. 当您使用<%= render @posts %> ,我确定这将在同一目录中调用带有前导_符号的部分文件。 Please ensure you have already file with the name _posts.html.erb . 请确保您已经使用_posts.html.erb IF you want to pass the value into partial, here i give you simple illustration : 如果您要将值传递给partial,在这里我给您简单的说明:

Assume you have this in customer_controller : 假设您在customer_controller具有以下内容:

def show
 @customer= Customer.find(params[:id])
end

Then it instance will available on show.html.erb : 然后它的实例将在show.html.erbshow.html.erb

<p>Item: <%= @customer.name%></p>

These instance variables are available in layouts and partials as well, instead pass variables to partials as locals like: 这些实例变量在布局和局部视图中也可用,而是将变量作为局部变量传递给局部视图,例如:

// here is the same concept with your problem
// render "cust_name" find the filename leading with underscore
<%= render "cust_name", name: @customer.name%>

Above line of code will call the file with name _cust_name.html.erb that contains : 上面的代码行将调用名为_cust_name.html.erb的文件,其中包含:

<h1><%= name %></h1>

Hope this helped. 希望这会有所帮助。

I am assuming that in your posts_controller.rb file you have specified the following: 我假设在您的posts_controller.rb文件中指定了以下内容:

def index
  @posts = Post.all
end

The reason why you are getting this error is because Rails is trying to display a partial for the variable @posts . 之所以出现此错误,是因为Rails试图显示变量@posts部分内容 What is implicit here, is that Rails is looking for a file named _post.html.erb in the folder app/views/posts/ and not finding anything. 这里隐含的是,Rails在文件夹app/views/posts/寻找名为_post.html.erb的文件,但未找到任何内容。 To fix this, you will have to manually create the file in that directory ( Note that all partials begin with an underscore ). 要解决此问题,您将必须在该目录中手动创建文件( 请注意,所有部分均以下划线开头 )。

Now, if you are wondering what you should put in this partial, first you should know what <%= render @posts %> is doing. 现在,如果您想知道应该在此部分中放入什么,首先应该知道<%= render @posts %>在做什么。 When it goes to the partial, it is iterating over all your posts and for each of them, it is going to do something. 当涉及局部时,它将遍历您的所有帖子,对于每个帖子,它都会做某事。 So your partial may look something like this: 所以您的部分可能看起来像这样:

<p>
  <%= link_to post.title, post_path(post.id) %>
</p>
<p>
  <%= link_to "Edit", edit_post_path %>
</p>
<p>
  <%= link_to "Delete", post_path(post.id), method: :delete %>
</p>

Just know that what is implicit here, is that we already have a @posts.each do |post| 只知道这里隐含的是,我们已经有一个@posts.each do |post| given for us, so you only have to define whatever content you wish to display for each individual post. 为我们提供的信息,因此您只需定义希望为每个帖子显示的内容即可。

Hope this helps!! 希望这可以帮助!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的Rails服务器无法运行? 我收到文件丢失的错误消息 - Why won't my Rails server run? I get an error that a file is missing 嘿,我正在尝试在 heroku 上部署一个 Rails 应用程序,当我运行 git push heroku master 时收到以下错误消息 - hey I am trying to deploy a rails app on heroku, and i get the follow error message when i run git push heroku master 如何获取我的Rails应用程序的基本URL(例如http:// localhost:3000)? - How do I get the base URL (e.g. http://localhost:3000) of my Rails app? 我在localhost上运行服务器时出现问题(ruby on rails) - Problem when I run my server on localhost (ruby on rails) 当我尝试在Rails应用程序中调用Amazon api时,为什么会收到错误消息“ HTTP Response:403 Forbidden”? - Why do I get the error 'HTTP Response: 403 Forbidden' when I try to call the amazon api in my Rails app? 制作新的Rails应用程序时,出现以下错误:`require&#39;:无法加载此类文件-捆绑程序(LoadError) - When making a new Rails app, I get the following error: `require': cannot load such file — bundler (LoadError) 在我的Rails应用程序中,创建新产品时如何自动附加用户ID? - In my rails app, how can I automatically attach a userID when a new product is created? 如何修复错误 Bootsnap::LoadPathCache::FallbackScan: 当我运行 rails new app - How can i fix the error Bootsnap::LoadPathCache::FallbackScan: when i run rails new app 我无法在Rails中捆绑安装新应用,它会显示以下错误消息 - I am not able to bundle install my new app in Rails it show me following error message 我可以将rails应用程序加载到heroku,但是当我使用heroku open时出现应用程序错误 - I can load my rails app to heroku, but when I use heroku open I get an Application Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM