2013-07-26 61 views
0

我下面這個教程的軌道:http://guides.rubyonrails.org/getting_started.html,我已經到了部分在您改線5.2軌的form_for網址:posts_path錯誤

<%= form_for :post do |f| %> 

<%= form_for :post, url: posts_path do |f| %> 

每次我改變它,我得到這個錯誤:

SyntaxError in Posts#new 

Showing /home/hiram/rails/blog/app/views/posts/new.html.erb where line #2 raised: 

compile error 
/home/hiram/rails/blog/app/views/posts/new.html.erb:2: syntax error, unexpected ':', expecting kEND 
....append= form_for :post, url: posts_path do |f| @output_buf... 
          ^
Extracted source (around line #2): 

1: <h1>New Post</h1> 
2: <%= form_for :post, url: posts_path do |f| %> 
3: <p> 
4: <%= f.label :title %><br> 
5: <%= f.text_field :title %> 
Trace of template inclusion: app/views/posts/new.html.erb 

Rails.root: /home/hiram/rails/blog 

回答

2

這是因爲url: posts_path是Ruby 1.9語法。如果你得到這個錯誤,那麼你必須使用Ruby 1.8。

你將不得不使用對Ruby 1.8的:url => posts_path語法:

<%= form_for :post, :url => posts_path do |f| %> 

你可以閱讀更多關於Ruby的語法哈希和herehere

我應該注意到,Ruby 1.8不再被支持,所以你應該更新到Ruby 1.9。如果你更新,你可以使用url: posts_path:url => posts_path語法 - Ruby 1.9可以同時使用它們。