今天发现了个奇怪的问题。
在我按照书中代码临摹的时候,报了莫名其妙的语法问题。
代码如下:
<%= form_remote_tag :url => { :action => :add_to_cart , :id => product} do %>
<%= submit_tag "Add to Cart" %>
<% end %>
错误:
SyntaxError in Store#index
Showing app/views/store/index.rhtml where line #9 raised:
compile error
./script/../config/../app/views/store/index.rhtml:9: syntax error, unexpected ')'
_erbout.concat " "; _erbout.concat(( form_remote_tag :url => { :action => :add_to_cart , :id => product } do ).to_s); _erbout.concat "\n"
^
./script/../config/../app/views/store/index.rhtml:13: syntax error, unexpected kEND, expecting ')'
end ; _erbout
^
查了大概两个小时,终于在一个论坛中某人2006年发表的一个求助帖中找到了答案,原因是rails版本变了之后语法发生了改变。而我恰巧没有按照书中的版本进行(看来了解版本间的变更很必要啊!)。
最终解决方案是:
<%= form_remote_tag :url => {:action => :add_to_cart , :id => product} %>
<%= submit_tag "Add to Cart" %>
<%= end_form_tag %>
不过说实话语义确实比以前明了。 原文:
http://forums.devshed.com/ruby-programming-142/form-remote-tag-not-displaying-button-411613.html