Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 缺少创建新视图轨道的模板_Html_Ruby On Rails_Ruby_Ruby On Rails 3 - Fatal编程技术网

Html 缺少创建新视图轨道的模板

Html 缺少创建新视图轨道的模板,html,ruby-on-rails,ruby,ruby-on-rails-3,Html,Ruby On Rails,Ruby,Ruby On Rails 3,我遵循了一个rails教程,该教程让您创建了一个简单的博客,作为rails的介绍。现在,我想添加另一个页面,但我遇到了一些问题 因此,我有一个页面列出了位于apps\view\articles\index.html.erb中的所有博客文章。代码如下所示: <table> <tr> <th>Title</th> <th>Preview</th> <th colspan="3"></

我遵循了一个rails教程,该教程让您创建了一个简单的博客,作为rails的介绍。现在,我想添加另一个页面,但我遇到了一些问题

因此,我有一个页面列出了位于apps\view\articles\index.html.erb中的所有博客文章。代码如下所示:

<table>
  <tr>
    <th>Title</th>
    <th>Preview</th>
    <th colspan="3"></th>
  </tr>

  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text[0,20] %></td>
      <td><%= link_to 'Read more...', article_path(article) %></td>
    </tr>
  <% end %>
</table>
4.在app\controllers中,我在教程中创建了Articles控制器的副本,只是我将类更改为:

class BackendController < ApplicationController
问题:
现在,当我单击该链接时,出现以下错误:

Missing template backend/index, application/index with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "C:/tutorialpath/app/views"

我读到一个类似的问题,但答案似乎是在app/views文件夹中创建一个新的html.erb文件(对于这个项目,它位于apps/views/articles中),我就是这么做的。是否有人有任何其他建议或看到我做错了什么?

如果您正在路由到后端#索引(您正在调用
后端控制器中的
index
方法),您的html.erb文件需要是
后端
文件夹中的
index.html.erb
后端/index.html.erb
如果您要路由到后端#index(您正在调用
后端控制器中的
index
方法),您的html.erb文件需要是
后端
文件夹中的
index.html.erb
后端/index.html.erb
您可以从控制器显式呈现任何视图,如下所示:

class BackendController
  def index
   @articles = Article.all
   render 'articles/index'
  end
end
而不是寻找
控制器/动作{format}.[erb | slim | haml | jbuilder]
的Rails隐式呈现。所以在本例中,
backend/index.html.erb

但是,在这种情况下,您可能希望使用单独的视图和部分来提取可重用的块

app/views/acticles/_article.html.erb:

<tr>
  <td><%= article.title %></td>
  <td><%= article.text[0,20] %></td>
  <td><%= link_to 'Read more...', article_path(article) %></td>
</tr>
<table>
  <tr>
    <th>Title</th>
    <th>Preview</th>
    <th colspan="3"></th>
  </tr>
  <%= render @articles %>
</table>
<table>
  <tr>
    <th>Title</th>
    <th>Preview</th>
    <th colspan="3"></th>
  </tr>
  <%= render @articles %>
</table>

请注意,文件名以下划线开头,表示该文件是部分视图而不是完整视图

app/views/acticles/index.html.erb:

<tr>
  <td><%= article.title %></td>
  <td><%= article.text[0,20] %></td>
  <td><%= link_to 'Read more...', article_path(article) %></td>
</tr>
<table>
  <tr>
    <th>Title</th>
    <th>Preview</th>
    <th colspan="3"></th>
  </tr>
  <%= render @articles %>
</table>
<table>
  <tr>
    <th>Title</th>
    <th>Preview</th>
    <th colspan="3"></th>
  </tr>
  <%= render @articles %>
</table>

标题
预览
app/views/backend/index.html.erb:

<tr>
  <td><%= article.title %></td>
  <td><%= article.text[0,20] %></td>
  <td><%= link_to 'Read more...', article_path(article) %></td>
</tr>
<table>
  <tr>
    <th>Title</th>
    <th>Preview</th>
    <th colspan="3"></th>
  </tr>
  <%= render @articles %>
</table>
<table>
  <tr>
    <th>Title</th>
    <th>Preview</th>
    <th colspan="3"></th>
  </tr>
  <%= render @articles %>
</table>

标题
预览
循环浏览文章,并为每一篇文章呈现部分内容-神奇!这相当于:

<% @articles.each do |a| %>
  <%= render partial: 'articles/article', article: a %>
<% end %>

现在,您可以更进一步,将整个表提取为部分表

您可以从控制器显式渲染任何视图,如下所示:

class BackendController
  def index
   @articles = Article.all
   render 'articles/index'
  end
end
而不是寻找
控制器/动作{format}.[erb | slim | haml | jbuilder]
的Rails隐式呈现。所以在本例中,
backend/index.html.erb

但是,在这种情况下,您可能希望使用单独的视图和部分来提取可重用的块

app/views/acticles/_article.html.erb:

<tr>
  <td><%= article.title %></td>
  <td><%= article.text[0,20] %></td>
  <td><%= link_to 'Read more...', article_path(article) %></td>
</tr>
<table>
  <tr>
    <th>Title</th>
    <th>Preview</th>
    <th colspan="3"></th>
  </tr>
  <%= render @articles %>
</table>
<table>
  <tr>
    <th>Title</th>
    <th>Preview</th>
    <th colspan="3"></th>
  </tr>
  <%= render @articles %>
</table>

请注意,文件名以下划线开头,表示该文件是部分视图而不是完整视图

app/views/acticles/index.html.erb:

<tr>
  <td><%= article.title %></td>
  <td><%= article.text[0,20] %></td>
  <td><%= link_to 'Read more...', article_path(article) %></td>
</tr>
<table>
  <tr>
    <th>Title</th>
    <th>Preview</th>
    <th colspan="3"></th>
  </tr>
  <%= render @articles %>
</table>
<table>
  <tr>
    <th>Title</th>
    <th>Preview</th>
    <th colspan="3"></th>
  </tr>
  <%= render @articles %>
</table>

标题
预览
app/views/backend/index.html.erb:

<tr>
  <td><%= article.title %></td>
  <td><%= article.text[0,20] %></td>
  <td><%= link_to 'Read more...', article_path(article) %></td>
</tr>
<table>
  <tr>
    <th>Title</th>
    <th>Preview</th>
    <th colspan="3"></th>
  </tr>
  <%= render @articles %>
</table>
<table>
  <tr>
    <th>Title</th>
    <th>Preview</th>
    <th colspan="3"></th>
  </tr>
  <%= render @articles %>
</table>

标题
预览
循环浏览文章,并为每一篇文章呈现部分内容-神奇!这相当于:

<% @articles.each do |a| %>
  <%= render partial: 'articles/article', article: a %>
<% end %>

现在,您可以更进一步,将整个表提取为部分表