缺少创建新视图的模板 Rails
Missing Template Creating New View Rails
我遵循了 rails 教程 here,该教程让您创建了一个简单的博客作为对 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>
现在,我想在那个名为 "Backend" 的页面上添加一个 link,它与上面代码中的页面完全相同,只是它有 "Backend" 作为 header(我稍后会在该页面分别添加不同的功能)。
在index.table上面html.erb,我写了:
- 在 app\views\articles 中,我创建了一个 backend.html.erb 文件,它与 index.html.erb 文件完全相同。
3.Inapp\helpers,我用下面的代码创建了一个backend_helper.rb:
module BackendHelper
end
4.In app\controllers,我在教程中创建了 Articles 控制器的副本,除了我将 class 更改为:
class BackendController < ApplicationController
5.And 在我的 routes.rb 文件中,我为后端添加了一个获取:
root 'welcome#index'
resources :articles do
root 'articles#index'
end
resources :articles do
resources :comments
end
get 'backend', to: 'backend#index'
问题:
现在,当我单击 link 时,出现以下错误:
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"
我读到过类似的问题 here,但似乎答案是在 app/views 文件夹中创建一个新的 html.erb 文件(对于这个项目,它位于apps/views/articles) 我做到了。有没有人有任何其他建议或看到我做错了什么?
如果您要路由到后端#index(您正在 BackendController
中调用 index
方法),您的 html.erb 文件需要在 index.html.erb
backend
文件夹:backend/index.html.erb
您可以显式从您的控制器渲染任何视图,如下所示:
class BackendController
def index
@articles = Article.all
render 'articles/index'
end
end
而不是 Rails 隐式 呈现寻找 controller/action.{format}.[erb|slim|haml|jbuilder]
。所以在这种情况下 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>
请注意,文件名以下划线开头,表示该文件是部分视图而非完整视图。
app/views/acticles/index.html.erb:
<table>
<tr>
<th>Title</th>
<th>Preview</th>
<th colspan="3"></th>
</tr>
<%= render @articles %>
</table>
app/views/backend/index.html.erb:
<table>
<tr>
<th>Title</th>
<th>Preview</th>
<th colspan="3"></th>
</tr>
<%= render @articles %>
</table>
<%= render @articles %>
遍历文章并呈现每个文章的部分内容 - 神奇!相当于:
<% @articles.each do |a| %>
<%= render partial: 'articles/article', article: a %>
<% end %>
现在您可以更进一步,将整个 table 也提取为部分。
我遵循了 rails 教程 here,该教程让您创建了一个简单的博客作为对 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>
现在,我想在那个名为 "Backend" 的页面上添加一个 link,它与上面代码中的页面完全相同,只是它有 "Backend" 作为 header(我稍后会在该页面分别添加不同的功能)。
在index.table上面html.erb,我写了:
- 在 app\views\articles 中,我创建了一个 backend.html.erb 文件,它与 index.html.erb 文件完全相同。
3.Inapp\helpers,我用下面的代码创建了一个backend_helper.rb:
module BackendHelper
end
4.In app\controllers,我在教程中创建了 Articles 控制器的副本,除了我将 class 更改为:
class BackendController < ApplicationController
5.And 在我的 routes.rb 文件中,我为后端添加了一个获取:
root 'welcome#index'
resources :articles do
root 'articles#index'
end
resources :articles do
resources :comments
end
get 'backend', to: 'backend#index'
问题:
现在,当我单击 link 时,出现以下错误:
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"
我读到过类似的问题 here,但似乎答案是在 app/views 文件夹中创建一个新的 html.erb 文件(对于这个项目,它位于apps/views/articles) 我做到了。有没有人有任何其他建议或看到我做错了什么?
如果您要路由到后端#index(您正在 BackendController
中调用 index
方法),您的 html.erb 文件需要在 index.html.erb
backend
文件夹:backend/index.html.erb
您可以显式从您的控制器渲染任何视图,如下所示:
class BackendController
def index
@articles = Article.all
render 'articles/index'
end
end
而不是 Rails 隐式 呈现寻找 controller/action.{format}.[erb|slim|haml|jbuilder]
。所以在这种情况下 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>
请注意,文件名以下划线开头,表示该文件是部分视图而非完整视图。
app/views/acticles/index.html.erb:
<table>
<tr>
<th>Title</th>
<th>Preview</th>
<th colspan="3"></th>
</tr>
<%= render @articles %>
</table>
app/views/backend/index.html.erb:
<table>
<tr>
<th>Title</th>
<th>Preview</th>
<th colspan="3"></th>
</tr>
<%= render @articles %>
</table>
<%= render @articles %>
遍历文章并呈现每个文章的部分内容 - 神奇!相当于:
<% @articles.each do |a| %>
<%= render partial: 'articles/article', article: a %>
<% end %>
现在您可以更进一步,将整个 table 也提取为部分。