(唯一的区别是,在第二个版本中,我试图用parens()调用form_form,第一个例子我没有使用parens。)我知道我可以清理我这样做的方式,但是被另一个线索所覆盖。
这项工作:
<%= 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| %>
但这不是:(尝试使用form_for作为函数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| %>
错误
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
答案 0 :(得分:1)
我不知道为什么解析器无法识别您要执行的操作。
为什么不尝试使代码可读,而不是试图找出ruby解析器的所有复杂性?
最简单的方法可能是使用||=
运算符,并将此逻辑移动到控制器中。
比较第一个示例的扩展形式:
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| %>
致:
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| %>
这样你可以使用no-parens样式而不会让令人困惑的?:
位堵塞一切。
希望有所帮助。
*顺便说一句,这是:
我认为出了什么问题
form_for()
不一定与form_for ()
相同,因此可能会对您是否试图说出来感到困惑
“调用form_for
,结果为(@quiz_attempt, :url => url) #which is not valid syntax
”
或
“使用参数form_for
调用(@quiz_attempt, {:url => url}) #which is what the real arguments look like
。