簡體   English   中英

rails form_for url:posts_path錯誤

[英]rails form_for url: posts_path error

我正在關注rails的這個教程: http//guides.rubyonrails.org/getting_started.html我已經到了5.2中你改變線的部分

<%= 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

那是因為url: posts_path是Ruby 1.9語法。 如果您收到該錯誤,那么您必須使用Ruby 1.8。

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

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

您可以在此處此處閱讀有關Ruby哈希語法的更多信息。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM