开发者

This Rails code works in Ruby 1.8.7 but not 1.9.2

Edit:

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

As stated in subject, the code runs perfectly in Ruby 1.8.7, but has errors in 1.9.2, can't seem to figure out why.

Code:

<%= 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)%>
  开发者_如何学JAVA      <%= f.hidden_field(:started)%>
        <%= submit_tag("Submit Quiz")%>
    <% end %>
</div>
<% end %>
<%= render :partial => 'course_steps/footer' %>

Error message:

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>


If you split things up a little in that ternary to make both sides easier to read, the problem becomes clear. Consider this...

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

That :url part of the hash just doesn't belong. The brackets are in the wrong place. I assume this is more along the lines of what you're wanting to achieve:

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

But a much nicer way of dealing with this would be to set @quiz_attempt properly in your controller so you can write the form more nicely:

# 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜