(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.
Related
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| %>
I have this form in a view in my project. I need to pass the task_id to a certain controller, but the log does not seem to be receiving the parameters. I don't know what the problem is.
<%= form_for :taskid, :url => {:action=>"index", :controller=>"statistics"}, :html => {:class => "nifty_form", :method => "GET"} do |f| %>
<%f.hidden_field :task_id, :value => task.id%>
<td><%= f.submit "اختر مهمة لاظهار احصائياتها منفرده"%></td>
<% end %>
You are missing on = after <%. The equal sign is needed whenever you want to the result appears on the HTML, so it is used with the field tags methods or render, for instance. You should not use the equal when using a if, for example, because this is not what you want to print (well, it can be, but most likely it isn't)
<%= form_for :taskid, :url => {:action=>"index", :controller=>"statistics"}, :html => {:class => "nifty_form", :method => "GET"} do |f| %>
<%= f.hidden_field :task_id, :value => task.id%>
<td><%= f.submit "اختر مهمة لاظهار احصائياتها منفرده"%></td>
<% end %>
However, as #AntonGrigoriev pointed out, you should use a object if you have, like this
<%= form_for #task, :url => {:action=>"index", :controller=>"statistics"}, :html => {:class => "nifty_form", :method => "GET"} do |f| %>
or you can simply use the hidden_field_tag
<%= hidden_field_tag :task_id, task.id %>
Hi please test with following code to send hidden value in rails, I have tried and worked for one of my application :
hidden_field_tag(name, value = nil, options = {}) public
eg:
<%= hidden_field_tag(:field_name,value=#offer_status)%>
I'm having a hard time understanding the difference the two lines bellow. The only difference is the usage of :url =>
1- The following line in my view is generating an error (removing form_for removes the error and rendering is done with no error):
<%= form_for( [#company, #appointment], :action => 'company_edit', :html => {:class => "form-horizontal"}) do |f| %>
I spent a lot of time trying to make sense of the error, but I do not know where 'company_edit'is coming from.
ActionView::Template::Error (undefined method `company_appointment_path' for #<#<Class:0x5047cc8>:0x5045070>):
9: </div>
10: <div class="widget-content nopadding">
11: <!--form action="#" method="get" class="form-horizontal"-->
12: <%= form_for([#company, #appointment], :action => 'company_edit', :html => {:class => "form-horizontal"}) do |f| %>
13: <% if #appointment.errors.any? %>
14: <div class="control-group">
15: <div id="error_explanation">
app/views/appointments/company_edit.html.erb:12:in `_app_views_appointments_company_edit_html_erb___287242072_42128364'
Here is a snippet of my route file:
resources :companies do
resources :appointments, only: [:company_edit] do
member do
get 'company_edit', :as => :company_edit
end
end
end
2- I found that using :url, made all work again, but why?
<%= form_for([#company, #appointment], :url => {:action => 'company_edit'}, :html => {:class => "form-horizontal"}) do |f| %>
I'm trying to understand this since I use 'form_for' in many places and I have never used ':url =>' before and I want to know if I was not using 'form_for' correctly and that I now need to update my code to properly use the helper.
EDIT
This is my controller:
def company_edit
#appointment = Appointment.find(params[:id])
#company = Company.find(params[:company_id])
end
I'm afraid the answer to your question is no.
:action is not an options parameter recognized by form_for.
Here's the API:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for
:url is an option it recognizes and itself includes an options hash which includes :action.
I hope that helps.
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.
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".