我正在尝试用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是正确的。我创建了表单以与我放入控制器的新动作相关联。我该如何解决这个错误?
答案 0 :(得分:2)
您缺少一个结束标记
你有
<% form_for :contact, :url => { action => 'create' } do |f|
您缺少%>
和操作符号:
<% form_for :contact, :url => { :action => 'create' } do |f| %>
编辑:您可以在sytax错误中看到这一点。如果您在错误中引用的特定行找不到任何错误,您首先应该查看它上面的行。
答案 1 :(得分:0)
您只是错过了%>
将第一行更改为以下内容:
<% form_for :contact, :url => { action => 'create' } do |f| %>
答案 2 :(得分:0)
试试这个: -
<%= form_for :contact, :url => { action => 'create' } do |f| %>