Rails 5部分形式未在生产中显示但正在开发中

时间:2018-04-18 14:01:24

标签: ruby-on-rails

这是相关代码。这一切都可以在我的开发服务器上运行,并直接复制到生产服务器。开发工作正常。我对html或css做的不多,所以它可能就在那里。有人看到明显的东西?我已经在论坛中检查了其他类似问题的建议,但我没有看到它。 来自考试控制器的代码:

  def new
    @exam = Exam.new
    @exam.questions.build if @exam.questions.blank?
  end

  # GET /exams/1/edit
  def edit
  end

  # POST /exams
  # POST /exams.json
  def create
      @exam = Exam.new(exam_params)
      respond_to do |format|
      if @exam.save
        format.html { redirect_to @exam, notice: 'Exam was successfully created.' }
        format.json { render :show, status: :created, location: @exam }
      else
        format.html { render :new }
        format.json { render json: @exam.errors, status: :unprocessable_entity }
      end
    end
  end

_form

<%= form_for(@exam) do |f| %>
  <% if @exam.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(exam.errors.count, "error") %> prohibited this exam from being saved:</h2>

      <ul>
      <% @exam.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <div class="field">
    <%= f.label :title %>:
    <%= f.text_field :title,:size =>"50" %>
  </div>
  <div class="field">
       <%= f.check_box :pcredit %>
       <%= f.label :pcredit, "Give partial credit when a question has multiple correct answers." %>
  </div>
  <div class="field">
    <%= f.check_box :available %>
    <%= f.label :available, "Available to take" %>
  </div>

<% exam.user_id = current_user.id %>
  <div class="field">
    <%= f.hidden_field :user_id, value: current_user.id %>
   </div>
   <div class="field">
    <%= f.hidden_field :department_id, value: current_user.department_id %>
   </div>
   <div class="field">
      <%= f.hidden_field :creator_id, value: current_user.id %>
   </div>
   <div class="field">
      <%= f.check_box :retake %>
      <%= f.label :retake, "Retakes allowed" %>
   </div>

  <div class="field">
    <%= f.check_box :results %>
    <%= f.label :results, "Show results" %>
  </div>
  <hr>
   <div id="questions">
      <%= f.fields_for :questions do |question| %>
        <%= render 'question_fields', f: question  %>
      <hr>
      <% end %>
  <%= link_to_add_association 'add_question', f, :questions,  partial: 'question_fields' %>
    </div> 
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

问题部分

<strong>Question:</strong>
<div class='nested-fields'>
  <div class="field">
    <%= f.label :name, "Question:" %>
    <%= f.text_area :name,:size =>"50" %>
  </div>

  <div class="field">
    <%= f.label :value, "Point Value:" %>
    <%= f.number_field :value %>
  </div>
  <div class="field">
    <%= f.label :available %>
    <%= f.check_box :available %>
  </div>
    <%= link_to_remove_association "remove question", f %>
     <div>
       <p><strong>Answers:</strong></p>
       <div id="answers">
         <%= f.fields_for :answers do |answer| %>
           <%= render 'answer_fields', f: answer  %> 
         <% end %>
       </div>
    <%= link_to_add_association 'add_answer', f, :answers,  partial: 'answer_fields' %>
        <hr>
     </div>
</div>  

回答部分

  <div class="field">
    <%= f.label :name, "Answer:" %>
    <%= f.text_area :name,:size =>"50" %>
  </div>
  <div class="field">
    <%= f.label :correct %>
    <%= f.check_box :correct %>
 </div>
   <div class="field">
    <%= f.hidden_field :creator_id, value: current_user.id %>
 </div>
 <div class="field">
    <%= f.hidden_field :department_id, value: current_user.department_id %>
 </div>

 <div class="field">
    <%= f.label :locked %>
    <%= f.check_box :locked %><br>
    <%= link_to_remove_association "remove answer", f %>
 </div> 

页面来源:

<!DOCTYPE html>
<html>
<head>
  <title>Online Placements</title>
  <link rel="stylesheet" media="all" href="/assets/exams-72737ac2bb269793edd945e3daa76d03d1bc74588f32b4acceb48ba3328ddc14.css" />
  <script src="/assets/application-3199215ebdb5afca46347c9bb159d59b78620065cea4336725d6fdeebc9e0e12.js"></script>
  <meta name="csrf-param" content="authenticity_token" />
  <meta name="csrf-token" content="+r+/jvc85b8QgSkJrpoT/idGMB3vYnkfLePBCjnWdsGBP1xhb/L86UA80toVa+/SXxGzb1KhDR0mm8hLkGt+Fw==" />
</head>
<body id="exams">



  <div id="banner"> 
    <img src=/assets/KHSlogo-3f9139495681edc2572d84a2d1157e33990d56e4536f98c1addaf5e9c092b2fc.gif width="160" height="77" alt="*****" />
    <title1>**************</title1>
    <p id="notice"></p>
  </div>
  <div id="columns">
    <div id="side">
      <font color="#ff0000">Home</font><br>

      <a href="/examinations">Placements</a>
      <br>
      <hr>

      <font color="#ff0000">Admins Only</font><br>
      <a href="/users">Users</a>
      <br>
      <hr>

      <font color="#ff0000">Documentation</font>

        <br><a target="_blank" href="https://docs.google.com/document/d/11_WLL7wZIkzssubus4XAQcouKkpRdrG6Lu-dWGn7gJY/edit#bookmark=id.8glqcm3gxwmz">User Document</a>
        <br><a target="_blank" href="https://docs.google.com/document/d/11_WLL7wZIkzssubus4XAQcouKkpRdrG6Lu-dWGn7gJY/edit#bookmark=id.o7htlx2i1tyx">Placement Help</a>

    </div>   
   <div id="main">
     <li> 
      Logged in as <b>****- </b>
      <a rel="nofollow" data-method="delete" href="/users/sign_out">Logout</a>   
     </li>
    <h1>Editing Exam: Placement Test</h1>

    <form class="edit_exam" id="edit_exam_1" action="/exams/1" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="_method" value="patch" /><input type="hidden" name="authenticity_token" value="K98vKBkYaUZL5WOt5xjlopkOVk7pvhc3HgBp5M0zmsQu3Hy2oBN5AzZjI3gTYcghP9qhXrpqAQenrZnYcVNw5Q==" />

  <div class="field">
    <label for="exam_title">Title</label>:
    <input size="50" type="text" value="Placement Test" name="exam[title]" id="exam_title" />
  </div>
  <div class="field">
       <input name="exam[pcredit]" type="hidden" value="0" /><input type="checkbox" value="1" checked="checked" name="exam[pcredit]" id="exam_pcredit" />
       <label for="exam_pcredit">Give partial credit when a question has multiple correct answers.</label>
  </div>
  <div class="field">
    <input name="exam[available]" type="hidden" value="0" /><input type="checkbox" value="1" checked="checked" name="exam[available]" id="exam_available" />
    <label for="exam_available">Available to take</label>
  </div>

  <div class="field">
    <input value="1" type="hidden" name="exam[user_id]" id="exam_user_id" />
   </div>
   <div class="field">
    <input value="1" type="hidden" name="exam[department_id]" id="exam_department_id" />
   </div>
   <div class="field">
      <input value="1" type="hidden" name="exam[creator_id]" id="exam_creator_id" />
   </div>
   <div class="field">
      <input name="exam[retake]" type="hidden" value="0" /><input type="checkbox" value="1" checked="checked" name="exam[retake]" id="exam_retake" />
      <label for="exam_retake">Retakes allowed</label>
   </div>

  <div class="field">
    <input name="exam[results]" type="hidden" value="0" /><input type="checkbox" value="1" checked="checked" name="exam[results]" id="exam_results" />
    <label for="exam_results">Show results</label>
  </div>
  <hr>

   <div id="questions">

  <a class="add_fields" data-association="question" data-associations="questions" data-association-insertion-template="&lt;strong&gt;Question:&lt;/strong&gt;
&lt;div class=&#39;nested-fields&#39;&gt;
  &lt;div class=&quot;field&quot;&gt;
    &lt;label for=&quot;exam_questions_attributes_new_questions_name&quot;&gt;Question:&lt;/label&gt;
    &lt;textarea name=&quot;exam[questions_attributes][new_questions][name]&quot; id=&quot;exam_questions_attributes_new_questions_name&quot; cols=&quot;50&quot;&gt;
&lt;/textarea&gt;
  &lt;/div&gt;

  &lt;div class=&quot;field&quot;&gt;
    &lt;label for=&quot;exam_questions_attributes_new_questions_value&quot;&gt;Point Value:&lt;/label&gt;
    &lt;input type=&quot;number&quot; name=&quot;exam[questions_attributes][new_questions][value]&quot; id=&quot;exam_questions_attributes_new_questions_value&quot; /&gt;
  &lt;/div&gt;
  &lt;div class=&quot;field&quot;&gt;
    &lt;label for=&quot;exam_questions_attributes_new_questions_available&quot;&gt;Available&lt;/label&gt;
    &lt;input name=&quot;exam[questions_attributes][new_questions][available]&quot; type=&quot;hidden&quot; value=&quot;0&quot; /&gt;&lt;input type=&quot;checkbox&quot; value=&quot;1&quot; name=&quot;exam[questions_attributes][new_questions][available]&quot; id=&quot;exam_questions_attributes_new_questions_available&quot; /&gt;
  &lt;/div&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;exam[questions_attributes][new_questions][_destroy]&quot; id=&quot;exam_questions_attributes_new_questions__destroy&quot; value=&quot;false&quot; /&gt;&lt;a class=&quot;remove_fields dynamic&quot; href=&quot;#&quot;&gt;remove question&lt;/a&gt;
     &lt;div&gt;
       &lt;p&gt;&lt;strong&gt;Answers:&lt;/strong&gt;&lt;/p&gt;
       &lt;div id=&quot;answers&quot;&gt;
                &lt;/div&gt;
    &lt;a class=&quot;add_fields&quot; data-association=&quot;answer&quot; data-associations=&quot;answers&quot; data-association-insertion-template=&quot;  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;
    &amp;lt;label for=&amp;quot;exam_questions_attributes_new_questions_answers_attributes_new_answers_name&amp;quot;&amp;gt;Answer:&amp;lt;/label&amp;gt;
    &amp;lt;textarea name=&amp;quot;exam[questions_attributes][new_questions][answers_attributes][new_answers][name]&amp;quot; id=&amp;quot;exam_questions_attributes_new_questions_answers_attributes_new_answers_name&amp;quot; cols=&amp;quot;50&amp;quot;&amp;gt;
&amp;lt;/textarea&amp;gt;
  &amp;lt;/div&amp;gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;
    &amp;lt;label for=&amp;quot;exam_questions_attributes_new_questions_answers_attributes_new_answers_correct&amp;quot;&amp;gt;Correct&amp;lt;/label&amp;gt;
    &amp;lt;input name=&amp;quot;exam[questions_attributes][new_questions][answers_attributes][new_answers][correct]&amp;quot; type=&amp;quot;hidden&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&amp;lt;input type=&amp;quot;checkbox&amp;quot; value=&amp;quot;1&amp;quot; name=&amp;quot;exam[questions_attributes][new_questions][answers_attributes][new_answers][correct]&amp;quot; id=&amp;quot;exam_questions_attributes_new_questions_answers_attributes_new_answers_correct&amp;quot; /&amp;gt;
 &amp;lt;/div&amp;gt;
   &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;
    &amp;lt;input value=&amp;quot;1&amp;quot; type=&amp;quot;hidden&amp;quot; name=&amp;quot;exam[questions_attributes][new_questions][answers_attributes][new_answers][creator_id]&amp;quot; id=&amp;quot;exam_questions_attributes_new_questions_answers_attributes_new_answers_creator_id&amp;quot; /&amp;gt;
 &amp;lt;/div&amp;gt;
 &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;
    &amp;lt;input value=&amp;quot;1&amp;quot; type=&amp;quot;hidden&amp;quot; name=&amp;quot;exam[questions_attributes][new_questions][answers_attributes][new_answers][department_id]&amp;quot; id=&amp;quot;exam_questions_attributes_new_questions_answers_attributes_new_answers_department_id&amp;quot; /&amp;gt;
 &amp;lt;/div&amp;gt;

 &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;
    &amp;lt;label for=&amp;quot;exam_questions_attributes_new_questions_answers_attributes_new_answers_locked&amp;quot;&amp;gt;Locked&amp;lt;/label&amp;gt;
    &amp;lt;input name=&amp;quot;exam[questions_attributes][new_questions][answers_attributes][new_answers][locked]&amp;quot; type=&amp;quot;hidden&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&amp;lt;input type=&amp;quot;checkbox&amp;quot; value=&amp;quot;1&amp;quot; name=&amp;quot;exam[questions_attributes][new_questions][answers_attributes][new_answers][locked]&amp;quot; id=&amp;quot;exam_questions_attributes_new_questions_answers_attributes_new_answers_locked&amp;quot; /&amp;gt;&amp;lt;br&amp;gt;
    &amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;exam[questions_attributes][new_questions][answers_attributes][new_answers][_destroy]&amp;quot; id=&amp;quot;exam_questions_attributes_new_questions_answers_attributes_new_answers__destroy&amp;quot; value=&amp;quot;false&amp;quot; /&amp;gt;&amp;lt;a class=&amp;quot;remove_fields dynamic&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;remove answer&amp;lt;/a&amp;gt;
 &amp;lt;/div&amp;gt; 
    &quot; href=&quot;#&quot;&gt;add_answer&lt;/a&gt;
        &lt;hr&gt;
     &lt;/div&gt;
&lt;/div&gt;    
" href="#">add_question</a>
    </div> 
  <div class="actions">
    <input type="submit" name="commit" value="Update Exam" data-disable-with="Update Exam" />
  </div>
</form>

<a href="/exams/1">Show</a> |
<a href="/exams">Back</a>
<br><br><br>

   </div>

 </div>   
</body>
</html>

2 个答案:

答案 0 :(得分:0)

@exam定义的位置看到控制器部件会很棒 我假设在您的prod情况下(表单未显示),关联questions为空。 所以有几种变体:

1.如果关联是空的,控制器总是会建立一个问题

@exam.questions.build if @exam.questions.blank?

2.您可以在视图中添加实体

f.fields_for :questions, @exam.questions.presence || [@exam.questions.build] do |question|

3.考虑到这种情况并显示正确的信息

<% if @exam.questions.present? %>
  <%= render 'questions_part' %>
<% else %>
  <%= render 'no_questions_partial' %>
<% end %>

我认为1种变体更好。

答案 1 :(得分:0)

好的,我终于明白了。我不得不预先编译资产。现在工作。 感谢那些提出建议的人。我非常感谢你的时间。