这个Rails代码适用于Ruby 1.8.7但不适用于1.9.2

时间:2011-03-21 16:05:15

标签: ruby-on-rails ruby ruby-on-rails-3

修改

这有效:

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

如主题所述,代码在Ruby 1.8.7中运行完美,但在1.9.2中有错误,似乎无法弄清楚原因。

代码

<%= render :partial => 'course_steps/header' %>
<% if QuizAttempt.patient_taken_quiz?(current_user.id, @course_step.step.step_quiz.id) %>
    <%= render :partial => 'course_steps/quiz_results' %>
<% else %>
<div id="QuizInstructions">
    <h3>Instructions</h3>
    <p><%= @course_step.step.step_quiz.instructions %> </p>
  </div>
<div id="Quiz">
    <%= 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| %>
    <%= render :partial => 'shared/error_messages', :object => f.object %>
        <% @course_step.step.step_quiz.step_quiz_questions.each do |quiz_question| %>
            <h3><%= quiz_question.value %></h3>
            <% quiz_question.step_quiz_question_choices.each do |quiz_question_choice| %>
            <%= radio_button_tag("quiz_attempt[quiz_questions][#{quiz_question.id}]", quiz_question_choice.id, f.object.get_quiz_question_choice(quiz_question.id) == quiz_question_choice.id)%>
            <%= quiz_question_choice.value %><br />
            <% end %>
        <% end %>
        <%= f.hidden_field(:patient_id)%>
        <%= f.hidden_field(:step_quiz_id)%>
        <%= f.hidden_field(:started)%>
        <%= submit_tag("Submit Quiz")%>
    <% end %>
</div>
<% end %>
<%= render :partial => 'course_steps/footer' %>

错误消息

Showing /Users/cmuench/rails_projects/infosurge/app/views/course_steps/show_quiz.html.erb where line #10 raised:

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

1 个答案:

答案 0 :(得分:1)

如果你在三元组中稍微分开一些东西以使双方更容易阅读,问题就会变得清晰。考虑一下......

new_quiz_attempt = QuizAttempt.new(:patient_id => current_user.id, :started => Time.now.utc, :step_quiz_id => @course_step.step.step_quiz.id)

form_for (@quiz_attempt.blank? ? new_quiz_attempt : @quiz_attempt, :url => submit_quiz_course_course_step_path(@course_step.course, @course_step)) do

那个:url的url部分不属于。括号位于错误的位置。我认为这更像是你想要实现的目标:

form_for (@quiz_attempt.blank? ? new_quiz_attempt : @quiz_attempt), :url => submit_quiz_course_course_step_path(@course_step.course, @course_step) do

但处理这个问题的一个更好的方法是在你的控制器中正确设置@quiz_attempt,这样你就可以更好地编写表单了:

# Controller
@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