RoR 应用程序中缺少模板 [英] Template is missing in RoR App

查看:31
本文介绍了RoR 应用程序中缺少模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 rails 中创建了一个应用程序guestbook",然后我使用rails g 控制器条目"创建了一个控制器.之后,我在 app/views/entries/中创建了 index.html,并在其中编写了以下内容:-

I created an app "guestbook" in rails, I then created a controller using "rails g controller entries". After that I created index.html in app/views/entries/ and in it I wrote following :-

 <h1>Hello <%= @name %></h1>
<%= form_tag :action => 'sign_in' do %>
<p>Enter your name:
<%= text_field_tag 'visitor_name', @name %></p>
<%= submit_tag 'Sign in' %>
<% end %>

在entries_controller.rb 中它是这样写的:-

and in entries_controller.rb it is written :-

class EntriesController < ApplicationController
    def sign_in
        @name = params[:visitor_name]
    end
end

在那之后,当我运行rails s"时并转到:-

after that when I run "rails s" and go to :-

localhost:3000/entries/

它向我展示了应该存在的正确视图.

it shows me a proper view which is supposed to be there.

当我输入名称并按下按钮时,它会路由到 localhost:3000/entries/sign_in 并显示以下错误:-

when I enter the name and press button it routes to localhost:3000/entries/sign_in and it says the following error :-

模板丢失

缺少模板条目/sign_in、带有{:locale=>[:en]、:formats=>[:html]、:handlers=>[:erb、:builder、:coffee]} 的应用程序/sign_in.搜索: * "/home/redblink/rbtest/guestbook/app/views"

Missing template entries/sign_in, application/sign_in with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/home/redblink/rbtest/guestbook/app/views"

请告诉我发生了什么???

please let me know what is happening ???

推荐答案

修复:

发生此错误是因为 Rails 服务器无法在 app/views/entries 目录中找到名称为 sign_in.html.erb 的模板.你应该创建它.

This error is occurred because rails server is unable to find template with name sign_in.html.erb in app/views/entries directory. You should create it.

说明:

默认情况下,rails 会尝试查找与您的操作名称相同的模板,即您的情况下的 sign_in.如果您不想创建或渲染此默认模板,则需要在操作中使用 render 方法指定另一个模板进行渲染.render 方法定义在 ActionController::Base 类中

By default rails tries to find template with same name as your action has i.e. sign_in in your case. If you don't want to create or render this default template, you need to specify another template to render by using render method in your action. render method is defined in ActionController::Base class

这篇关于RoR 应用程序中缺少模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆