【问题标题】:Rails 4.0.2 Getting missing template error, but the template is thereRails 4.0.2缺少模板错误,但模板在那里
【发布时间】:2014-01-08 19:17:45
【问题描述】:

铁路 4.0.2 红宝石 2.0 mysql 网砖

我的控制器代码是

class ContactController < ApplicationController

def index
@contacts = Contact.find(:all)
end

def show
end

def new
end

def create
end

def update
end

end

在 routes.rb 文件中我放了

resources :contact

我将 index.html.erb 文件放在 app/views/contact 文件夹中,index.html.rb 文件的代码如下

<h1>My Contact List</h1>
<% if @contacts.blank? %>
<p>No contacts to display.</p>
<% else %>
<ul id=”contacts”>
<% @contacts.each do |c| %>
<li>
<%= link_to c.first_name+’ ‘+c.last_name,
{:action => ‘show’, :id => c.id} -%>
</li>
<% end %>
</ul>
<% end %>

即使 webrick 服务器运行良好,但我收到模板丢失错误。但是模板是存在的。任何建议。我是 ROR 的新手。 谢谢

【问题讨论】:

  • 为什么你的扩展名是.html.rb 而不是.html.erb
  • 对不起,这是拼写错误。文件扩展名为 .html.erb

标签: ruby-on-rails


【解决方案1】:

在联系人姓名中添加“s”
这是 CRUD 资源的 Rails 命名空间约定http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default

# app/controllers/contacts_contoller.rb
class ContactsController < ApplicationController

#routes.rb
resources :contacts

# app/views/contacts/index.html.erb
. . .

【讨论】:

  • 我更改为资源 :contacts 但现在我收到 No route matches [GET] "/contact/index" 错误。
  • 您是否将 ContactController 更改为 ContactsController?
  • 是的,因为我们有index方法,所以我们有很多联系人
猜你喜欢
  • 2015-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多