添加新模型后的SyntaxError

时间:2013-04-13 15:15:54

标签: ruby-on-rails-3 model-view-controller

大家好,我有一个应用程序调用演示。在演示中,主要的脚手架叫“联系”。现在我想在'contact#show'page中添加一个新模型'地址'(有5个属性)。这是我的contactcontroller定义show

def show
    @contact = Contact.find(params[:id])
    @addresses = @contact.addresses.paginate(page: params[:page])
end 

这是我试图在view / contacts / show.html.erb

中显示'地址'的地方
<%= form_for(@contact) do |f| %>
      <% if @contact.addresses.any? %>
      <h3>Addresses (<%= @contact.addresses.count %>)</h3>
      <ol >
        <%= render @addresses %>
      </ol>
      <%= will_paginate @addresses %>
      <% end %>
<%= link_to 'Edit', edit_contact_path(@contact) %> 
<%= link_to 'List', contacts_path %>

这是view / addresses / _address.html.erb(我不知道如何显示地址的所有属性,所以我只使用'address.all')

<li>
  <span class="content"><%= address.all %></span>
</li>

当我启动服务器并单击“显示”链接机器时,会给出错误消息,如

SyntaxError in Contacts#show
ActionView::Template::Error (/media/sf_VM_working/demo/app/views/contacts/show.html.erb:38: syntax error, unexpected keyword_ensure, expecting keyword_end
/media/sf_VM_working/demo/app/views/contacts/show.html.erb:40: syntax error, unexpected $end, expecting keyword_end):
    35: <%= link_to 'Edit', edit_contact_path(@contact) %> 
    36: <%= link_to 'List', contacts_path %>
  app/controllers/contacts_controller.rb:14:in `show'

有谁能告诉我哪里出错了,非常感谢你的帮助:)。

1 个答案:

答案 0 :(得分:0)

你错过了erb模板的结尾

看起来应该是这样的

<%= form_for(@contact) do |f| %>
  <% if @contact.addresses.any? %>
    <h3>Addresses (<%= @contact.addresses.count %>)</h3>
      <ol>
        <%= render @addresses %>
      </ol>
    <%= will_paginate @addresses %>
  <% end %>
<% end %> 
<%= link_to 'Edit', edit_contact_path(@contact) %> 
<%= link_to 'List', contacts_path %>

请注意form_for块的结束