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开发者_StackOverflow中文版...
... ^
/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
.
精彩评论