0

我正在尝试从 Rails 圣经中完成本教程,我创建了一个新的联系表格,并输入了书中的所有内容:

    <h1>Create New Contact</h1>
<% form_for :contact, :url => { action => 'create' } do |f|
  <p><label for="contact_first_name">First Name:</label>
  <%= f.text_field 'contact', 'first_name' %></p>

  <p><label for="contact_last_name">Last Name:</label>
  <%= f.text_field 'contact', 'last_name' %></p>

  <p><label for="contact_address">Address:</label>
  <%= f.text_field 'contact', 'address' %></p>

  <p><label for="contact_city">City:</label>
  <%= f.text_field 'contact', 'City' %></p>

  <p><label for="contact_state">State:</label>
  <%= f.text_field 'contact', 'state' %></p>

  <p><label for="contact_country">Country:</label>
  <%= f.text_field 'contact', 'country' %></p>

  <p><label for="contact_phone">Phone:</label>
  <%= f.text_field 'contact', 'phone' %></p>

  <p><label for="contact_email">Email:</label>
  <%= f.text_field 'contact', 'email' %></p>

  <%= f.submit "Create" %>
<% end %>  

<p><%= link_to 'Back', { :action => 'index' } %>

下面是我的控制器文件

    class ContactController < ApplicationController

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

  def show
  end

  def new
    @contact = Contact.new
  end
  def create
  end

  def update
  end
end

但我得到一个语法错误:

SyntaxError in Contact#new

Showing C:/rails_projects/contactlist/app/views/contact/new.html.erb where line #3 raised:

C:/rails_projects/contactlist/app/views/contact/new.html.erb:3: syntax error, unexpected '<'
  <p><label for="contact_first_name">First Name:</label>
   ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:3: syntax error, unexpected tLABEL, expecting keyword_do or '{' or '('
  <p><label for="contact_first_name">First Name:</label>
                                                ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:4: unknown regexp option - p
end pattern with unmatched parenthesis: /label>
  <%= f.text_field 'contact', 'first_name' ;@output_buffer.safe_concat('</
C:/rails_projects/contactlist/app/views/contact/new.html.erb:6: syntax error, unexpected '<'
  <p><label for="contact_last_name">Last Name:</label>
   ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:6: syntax error, unexpected tLABEL, expecting keyword_do or '{' or '('
  <p><label for="contact_last_name">Last Name:</label>
                                              ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:7: unknown regexp option - p
unmatched close parenthesis: /label>
  ');@output_buffer.append= ( f.text_field 'contact', 'last_name' );@output_buffer.safe_concat('</
C:/rails_projects/contactlist/app/views/contact/new.html.erb:9: syntax error, unexpected '<'
  <p><label for="contact_address">Address:</label>
   ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:9: syntax error, unexpected tLABEL
  <p><label for="contact_address">Address:</label>
                                          ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:10: unknown regexp option - p
unmatched close parenthesis: /label>
  ');@output_buffer.append= ( f.text_field 'contact', 'address' );@output_buffer.safe_concat('</
C:/rails_projects/contactlist/app/views/contact/new.html.erb:12: syntax error, unexpected '<'
  <p><label for="contact_city">City:</label>
   ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:12: syntax error, unexpected tLABEL
  <p><label for="contact_city">City:</label>
                                    ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:13: unknown regexp option - p
unmatched close parenthesis: /label>
  ');@output_buffer.append= ( f.text_field 'contact', 'City' );@output_buffer.safe_concat('</
C:/rails_projects/contactlist/app/views/contact/new.html.erb:15: syntax error, unexpected '<'
  <p><label for="contact_state">State:</label>
   ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:15: syntax error, unexpected tLABEL
  <p><label for="contact_state">State:</label>
                                      ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:16: unknown regexp option - p
unmatched close parenthesis: /label>
  ');@output_buffer.append= ( f.text_field 'contact', 'state' );@output_buffer.safe_concat('</
C:/rails_projects/contactlist/app/views/contact/new.html.erb:18: syntax error, unexpected '<'
  <p><label for="contact_country">Country:</label>
   ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:18: syntax error, unexpected tLABEL
  <p><label for="contact_country">Country:</label>
                                          ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:19: unknown regexp option - p
unmatched close parenthesis: /label>
  ');@output_buffer.append= ( f.text_field 'contact', 'country' );@output_buffer.safe_concat('</
C:/rails_projects/contactlist/app/views/contact/new.html.erb:21: syntax error, unexpected '<'
  <p><label for="contact_phone">Phone:</label>
   ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:21: syntax error, unexpected tLABEL
  <p><label for="contact_phone">Phone:</label>
                                      ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:22: unknown regexp option - p
unmatched close parenthesis: /label>
  ');@output_buffer.append= ( f.text_field 'contact', 'phone' );@output_buffer.safe_concat('</
C:/rails_projects/contactlist/app/views/contact/new.html.erb:24: syntax error, unexpected '<'
  <p><label for="contact_email">Email:</label>
   ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:24: syntax error, unexpected tLABEL
  <p><label for="contact_email">Email:</label>
                                      ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:25: unknown regexp option - p
unmatched close parenthesis: /label>
  ');@output_buffer.append= ( f.text_field 'contact', 'email' );@output_buffer.safe_concat('</
C:/rails_projects/contactlist/app/views/contact/new.html.erb:30: syntax error, unexpected '<', expecting keyword_end
<p>');@output_buffer.append= (...
 ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:30: syntax error, unexpected tCONSTANT, expecting keyword_end
..._buffer.append= ( link_to 'Back', { :action => 'index' } );@...
...                               ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:30: syntax error, unexpected tIDENTIFIER, expecting keyword_end
..._to 'Back', { :action => 'index' } );@output_buffer.to_s
...                               ^
C:/rails_projects/contactlist/app/views/contact/new.html.erb:30: unterminated string meets end of file
C:/rails_projects/contactlist/app/views/contact/new.html.erb:30: syntax error, unexpected $end, expecting keyword_end
Extracted source (around line #3):

1: <h1>Create New Contact</h1>
2: <% form_for :contact, :url => { action => 'create' } do |f|
3:   <p><label for="contact_first_name">First Name:</label>
4:   <%= f.text_field 'contact', 'first_name' %></p>
5:   
6:   <p><label for="contact_last_name">Last Name:</label>
Trace of template inclusion: app/views/contact/new.html.erb

Rails.root: C:/rails_projects/contactlist

Application Trace | Framework Trace | Full Trace
Request

Parameters:

None
Show session dump

Show env dump

Response

Headers:

None

我什至使用验证器来确保 HTML 是正确的。我创建了与我放入控制器中的新操作相关联的表单。我该如何解决这个错误?

4

3 回答 3

2

您缺少一个结束 rails 标记

你有

<% form_for :contact, :url => { action => 'create' } do |f|

您缺少%>和 动作符号:

<% form_for :contact, :url => { :action => 'create' } do |f| %>

编辑:您可以在语法错误中看到这一点。如果您找不到错误中引用的特定行有任何问题,您首先想到的应该是查看它上面的行。

于 2013-06-13T17:35:53.793 回答
0

你只是缺少一个%>

将第一行更改为以下内容:

<% form_for :contact, :url => { action => 'create' } do |f| %>
于 2013-06-13T17:44:51.723 回答
0

试试这个: -

<%= form_for :contact, :url => { action => 'create' } do |f| %>
于 2013-06-13T17:58:39.867 回答