开发者

Converting Rails 2 routes to Rails 3

Like in my first question from yesterday, I'm still doing that tutorial.

I've encountered another issue with the Rails 2 / Rails 3 routing differences.

So my question is: How do you "translate" this:

<%= form_remote_tag(:controller => "posts", :action => "create") do %>

to Rails 3 routing?

Edit: This is the error code I get :

Showing C:/Users/Lunasea/Web-Site/Standart/app/views/posts/_message_form.html.erb where line #5 raised: 

C:/Users/Lunasea/Web-Site/St开发者_Go百科andart/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

The content of _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 %>


You'd use a form_tag and pass :remote => true to it…

form_tag :url => {:controller => 'posts', :action => 'create'}, :remote => true

(Make sure you've included jQuery UJS or equivalent Prototype library, because Rails no longer includes the javascript inline like it used to.)


I cite here from the book Fernandez: The Rails 3 Way section 11.13

PrototypeHelper

PrototypeHelper has been heavily modified ... The following helper methods were removed and made available in an official Prototype Legacy Helper

  • ...
  • form_remote_for
  • form_remote_tag

That is the reason for your error. You have to translate that to the new syntax with the option :remote => true to indicate a remote call (AJAX).

So the following should work:

<%= form_tag({:controller => "posts", :action => "create"}, {:remote => true}) do %>
  ...
<% end %>

See the API for Rails and search there for form_tag for additional information.


Your problem is that you use <!-- and --> to hide old code. Rails will still execute that code. Use the following format instead:

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


form_remote_tag is not there in the Rails 3

Use

form_tag :url => {...}, :remote => true 

instead

What this means is that all previous remote_ helpers have been removed from Rails core and put into the Prototype Legacy Helper. To get UJS hooks into your HTML, you now pass :remote => true instead. For example:

form_for @post, :remote => true

http://edgeguides.rubyonrails.org/3_0_release_notes.html


The error is saying there is a syntax problem in the message form template. My guess is that you are missing a <% end %> to close the do in your form_remote_tag call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜