Converting Rails 2 routes to Rails 3 - ruby-on-rails

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/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
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.

Related

SimpleForm: SyntaxError in Controller

Any idea on what could be wrong in this form?
Error:
vehicleTrack.html.erb:141: syntax error, unexpected keyword_do_block, expecting => ... params[:rangefrom_string]} do |f| #output_buffer.safe_appe... ... ^
This is my view:
<%= simple_form_for '', url: convertTrackToArea_path, :method => :post,
{ :controller => "vehicles",
:action => "convertTrackToArea",
:search => params[:search],
:rangefrom_string => params[:rangefrom_string]} do |f| %>
<%= f.input :areano, :label => 'Areano' %>
<%= f.button :submit, value: "Crear",:name => nil%>
<% end %>
I have not used simple_form so my guess is you are passing the last last argument incorrectly and it wont take a hash as argument and it detects the argument as key and searching for a value so throws error that => is missing, which is used to identify value in hash. So you can do something like this I suppose:
<%= simple_form_for '',
:url => url_for(:action => 'convertTrackToArea', :controller => 'vehicles',:search => params[:search],
:rangefrom_string => params[:rangefrom_string]),
:method => 'post' do |f| %>
since you are specifying controller and action you dont need to mention convertTrackToArea_path . If that route is already setup, you can just use that like in your posted question and remove controller and action name like:
<%= simple_form_for '', :url => convertTrackToArea_path(:search => params[:search],:rangefrom_string => params[:rangefrom_string]),:method => 'post' do |f| %>

how to set class attribute with form_tag in rails

I have the following line of haml:
=form_tag :action => 'create', :controller => 'comments', :class => 'comment_form' do
But the html that gets output is:
<form accept-charset="UTF-8" action="/comments?class=comment_form" method="post"></form>
I want to set the class. How do I do this?
<-- Update -->
With this:
=form_tag ({ :action => 'create', :controller => 'comments' }, { :class => 'comment_form' }) do
I get this error:
syntax error, unexpected ',', expecting ')'
...', :controller => 'comments' }, { :class => 'comment_form' }...
<-- Second Update -->
The problem above is the space between 'form_tag' and '(' #woahdae's answer is correct
form_tag takes 2 options hashes, the first being passed to url_for, the second being passed to the form builder.
So, you have to do it like:
= form_tag({:action => 'create',...}, {:class => 'comment_form'}) do
otherwise Rails thinks all the key/value pairs are for url_for, which will append any keys it doesn't understand as query parameters.
This works for me:
form_tag named_route, :method => :put, :class => 'disable_on_submit'
With Rails 3.0.15
On Rails 5, you can do the following:
<%= form_tag(your_named_path, {class: 'form-inline'}) do %>
<% end %>
You can do follow as:
form_tag your_path, method: :get, id: "your_id", class: "your_class" do
end
In case you found this question and actually wanted to solve class naming for a form_for:
<%= form_for #task, html: {:class => "custom_class"} do |f| %>

Trouble passing locals in Partial

I have a partial called _avatar.html.erb
I want to pass in an id as a local variable called entity_id which will be the id of an object.
<% form_tag({:controller => "avatar", :action => "upload", :id => entity_id},
:multipart => true ) do %>
<fieldset>
<legend><%= title %></legend>
<% if avatar.exists? %>
<%= avatar_tag(avatar) %>
[<%= link_to "delete", {:controller => "avatar",:action => "delete",
:id => entity_id},:confirm => "Are you sure" %>]
...
Here is the call for the parital:
<%= render :partial => 'avatar/avatar', :locals => {:avatar => #avatar,
:title => #title, :entity_id => #board.id } %>
When I try this I get the following errors:
undefined local variable or method `entity_id' for #<ActionView::Base:0x2736bb0>
When I take that out I also get an error telling me it can't find the local variable "title".
Can anyone help this seems to be the correct way to do this.
Thanks in advance
Try using :id=>#entity_id (note the # symbol preceding the name). Likewise, #title, instead of "title".

Ruby on rails form_for oddness

(The only difference is that in the second version I am trying to call form_form with parens () and the first example I am not using parens.) I know I could clean up the way I am doing this, but that has been covered by a different thread.
THIS WORKS:
<%= form_for (#quiz_attempt.blank? ? QuizAttempt.new(:patient_id => current_user.id, :started => Time.now.utc, :step_quiz_id => #course_step.step.step_quiz.id) : #quiz_attempt), :url => submit_quiz_course_course_step_path(#course_step.course, #course_step) do |f| %>
BUT THIS DOESN'T: (Trying to use form_for with as a function form_for()
<%= form_for ((#quiz_attempt.blank? ? QuizAttempt.new(:patient_id => current_user.id, :started => Time.now.utc, :step_quiz_id => #course_step.step.step_quiz.id) : #quiz_attempt), :url => submit_quiz_course_course_step_path(#course_step.course, #course_step)) do |f| %>
The ERROR
ERROR:/Users/cmuench/rails_projects/infosurge/app/views/course_steps/show_quiz.html.erb:10: syntax error, unexpected ',', expecting ')'
...step_quiz.id) : #quiz_attempt), :url => submit_quiz_course_c...
... ^
/Users/cmuench/rails_projects/infosurge/app/views/course_steps/show_quiz.html.erb:10: syntax error, unexpected ')', expecting keyword_end
...rse_step.course, #course_step)) do |f| #output_buffer.safe_c...
... ^
/Users/cmuench/rails_projects/infosurge/app/views/course_steps/show_quiz.html.erb:27: syntax error, unexpected keyword_ensure, expecting $end
Extracted source (around line #10):
7: <p><%= #course_step.step.step_quiz.instructions %> </p>
8: </div>
9: <div id="Quiz">
10: <%= form_for ((#quiz_attempt.blank? ? QuizAttempt.new(:patient_id => current_user.id, :started => Time.now.utc, :step_quiz_id => #course_step.step.step_quiz.id) : #quiz_attempt), :url => submit_quiz_course_course_step_path(#course_step.course, #course_step)) do |f| %>
11: <%= render :partial => 'shared/error_messages', :object => f.object %>
12: <% #course_step.step.step_quiz.step_quiz_questions.each do |quiz_question| %>
13: <h3><%= quiz_question.value %></h3>
Trace of template inclusion: app/views/course_steps/show_quiz.html.erb
I have no* idea why the parser doesn't recognize what you're trying to do.
Instead of trying to map out all the intricacies of ruby's parser, why not try to make the code readable?
The easiest way is probably to use the ||= operator, and move this logic into the controller.
Compare the expanded form of your first example:
Controller:
Not Shown
View:
<%= form_for (
(#quiz_attempt.blank? ?
QuizAttempt.new(
:patient_id => current_user.id,
:started => Time.now.utc,
:step_quiz_id => #course_step.step.step_quiz.id) :
#quiz_attempt
),
:url => submit_quiz_course_course_step_path(#course_step.course, #course_step)
) do |f| %>
To:
Controller:
#... near the end of the relevant action
#quiz_attempt ||= QuizAttempt.new(:patient_id => current_user.id,
:started => Time.now.utc,
:step_quiz_id => #course_step.step.step_quiz.id)
View:
<%= form_for #quiz_attempt, :url =>
submit_quiz_course_course_step_path(#course_step.course, #course_step) do |f| %>
That way you can use the no-parens style without having the confusing ?: bits clogging everything up.
Hope that helps.
*As an aside, here's:
What I think is going wrong
form_for() is not necessarily the same as form_for (), so it could be getting confused as to whether you're trying to say
"call form_for with the result of (#quiz_attempt, :url => url) #which is not valid syntax"
or
"call form_for with the arguments (#quiz_attempt, {:url => url}) #which is what the real arguments look like.

switch images with onclick

I have a partial called _avatar.html.erb
I want to pass in an id as a local variable called entity_id which will be the id of an object.
<% form_tag({:controller => "avatar", :action => "upload", :id => entity_id}, :multipart => true ) do %>
<fieldset>
<legend><%= title %></legend>
<% if avatar.exists? %>
<%= avatar_tag(avatar) %>
[<%= link_to "delete", {:controller => "avatar",:action => "delete", :id => entity_id},:confirm => "Are you sure" %>]
...
Here is the call for the parital:
<%= render :partial => 'avatar/avatar', :locals => {:avatar => #avatar, :title => #title, :entity_id => #board.id } %>
When I try this I get the following errors:
undefined local variable or method `entity_id' for #
When I take that out I also get an error telling me it can't find the local variable "title".
Can anyone help this seems to be the correct way to do this.
Thanks in advance
Are you sure the error is coming from the partial? You are using entity_id in the form_tag. Where is it being defined? title isn't used in the partial. It is used inside of legend though. Is that defined?

Resources