简体   繁体   English

该Rails代码在Ruby 1.8.7中有效,但在1.9.2中不起作用

[英]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与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. 如主题所述,该代码可以在Ruby 1.8.7中完美运行,但是在1.9.2中有错误,似乎无法弄清原因。

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)%>
        <%= 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. 哈希的:url部分不属于该对象。 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: 但是更好的处理方法是在控制器中正确设置@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM