[英]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.erb
上show.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.