就像我昨天的第一个问题一样,我仍然在做that tutorial。
我遇到了Rails 2 / Rails 3路由差异的另一个问题。
所以我的问题是:你如何“翻译”这个:
<%= form_remote_tag(:controller => "posts", :action => "create") do %>
到Rails 3路由?
修改:这是我得到的错误代码:
Showing C:/Users/Lunasea/Web-Site/Standart/app/views/posts/_message_form.html.erb where line #5 raised:
C:/Users/Lunasea/Web-Site/Standart/app/views/posts/_message_form.html.erb:5: syntax error, unexpected tASSOC, expecting '}'
...pend= form_tag {:controller => "posts", :action => "create"...
C:/Users/Lunasea/Web-Site/Standart/app/views/posts/_message_form.html.erb:5: syntax error, unexpected ',', expecting '}'
...rm_tag {:controller => "posts", :action => "create"}, :remot...
C:/Users/Lunasea/Web-Site/Standart/app/views/posts/_message_form.html.erb:5: syntax error, unexpected tASSOC, expecting keyword_end
...action => "create"}, :remote => true do @output_buffer.safe_...
C:/Users/Lunasea/Web-Site/Standart/app/views/posts/_message_form.html.erb:12: syntax error, unexpected keyword_ensure, expecting keyword_end
C:/Users/Lunasea/Web-Site/Standart/app/views/posts/_message_form.html.erb:14: syntax error, unexpected $end, expecting keyword_end
_message_form.html.erb
的内容:
<% if logged_in? %>
<!--<% form_for product, :url => {:action => 'add_to_cart', :id => product.id}, :remote => true do %>-->
<!--<%= form_remote_tag(:controller => "posts", :action => "create") do %>-->
<%= form_for{:controller => "posts", :action => "create"}, :remote => true do %>
<%= label_tag(:message, "What are you doing?") %><br />
<%= text_area_tag(:message, nil, :size => "60x2") %><br />
<%= submit_tag("Update") %>
<% end %>
<% end %>
答案 0 :(得分:2)
您使用了form_tag并将:remote => true
传递给它......
form_tag :url => {:controller => 'posts', :action => 'create'}, :remote => true
(确保您已包含jQuery UJS或等效的原型库,因为Rails不再像过去那样包含javascript内联。)
答案 1 :(得分:1)
我在这里引用Fernandez: The Rails 3 Way第11.13节
PrototypeHelper
PrototypeHelper已经过大量修改......删除了以下帮助方法并在官方Prototype Legacy Helper中提供
- ...
form_remote_for
form_remote_tag
这就是你错误的原因。您必须使用选项:remote => true
将其转换为新语法,以指示远程调用(AJAX)。
所以以下内容应该有效:
<%= form_tag({:controller => "posts", :action => "create"}, {:remote => true}) do %>
...
<% end %>
请参阅API for Rails并在此处搜索form_tag
以获取更多信息。
答案 2 :(得分:0)
错误是说消息表单模板中存在语法问题。我的猜测是你错过了&lt;%end%&gt;关闭form_remote_tag调用中的do。
答案 3 :(得分:0)
form_remote_tag
使用
form_tag :url => {...}, :remote => true
代替
这意味着所有以前的remote_助手都是 从Rails核心中删除并放入Prototype Legacy Helper。至 将UJS挂钩到你的HTML中,你现在传递:remote =&gt;相反,真的。 例如:
form_for @post,:remote =&gt;真
答案 4 :(得分:0)
您的问题是您使用<!--
和-->
来隐藏旧代码。 Rails仍将执行该代码。请改用以下格式:
<% if logged_in? %>
<%-# form_for product, :url => {:action => 'add_to_cart', :id => product.id}, :remote => true do %>
<%-#= form_remote_tag(:controller => "posts", :action => "create") do %>
<%= form_for{:controller => "posts", :action => "create"}, :remote => true do %>
<%= label_tag(:message, "What are you doing?") %><br />
<%= text_area_tag(:message, nil, :size => "60x2") %><br />
<%= submit_tag("Update") %>
<% end %>
<% end %>