Rails不呈现部分(_new_multiple_choice.rjs)

时间:2011-04-05 19:30:11

标签: ruby-on-rails rjs

#online_exam_controller
class OnlineExamController < ApplicationController
   def index
    @user=current_user
    @employee=Employee.find_by_employee_number(@user.username)
    # flash[:notice]="#{@employee.id}"
    @subjects=EmployeesSubject.find_all_by_employee_id(@employee.id)
    @subject_name=[]
    @batch_details=[]
    @display_details=[[]]
    @count=0
    for @t in @subjects
     @subject_name[@count]=Subject.find(@t.subject_id,:select=>'name,code,batch_id')
     @batch_details[@count]=Batch.find(@subject_name[@count].batch_id,:select =>'name')
     @display_details[@count][0]=@subject_name[@count].name+" "+@batch_details[@count].name
     @display_details[@count][1]=@t.subject_id
     @count+=1
    end
   end
   def show
    if params[:subject_id]==''
     @question_type=[]
    else
     @question_type=['multiple_choice','fill_ups','matches','descriptive']
     @subject_id=params[:subject_id]

   end
   respond_to do |format|
   format.js {render:action=>'show' }
   end
  end
  def new_multiple_choice
   @quiz=MultipleChoiceQuestion.new
   @subject=Subject.find params[:id] if request.xhr? and params[:id]
   respond_to do |format|
    format.js {render :action => 'new_multiple_choice'}
   end
  end
 end
#index.html.erb
<div id="content-header">
<img src="/images/show_settings.png" alt="Subjects"/>
<h1>Subjects</h1>
<h3>Home</h3>
 <div id="app-back-button">
 <%= link_to_function image_tag("/images/buttons/back.png",:border => 0), "history.back()" %>
 </div>
 </div>

 <div id="page-yield">
 <% unless flash[:notice].nil? %>
 <p class="flash-msg"> <%= flash[:notice] %> </p>
 <% end %>
 <div class="box">

  <div class="label-field-pair">
  <label for="student_course">Select a batch:
  <%= image_tag("loader.gif",:align => "absmiddle",:border => 0,:id => "loader", :style =>"display: none;" ) %>
  </label>
  <div class="text-input-bg">
  <%= select :subject,:id,
   @display_details.map {|b| [b[0],b[1]]},
  {:prompt => 'selecct subject'},
  {:onchange =>"#{remote_function(
  :url=>{:action => 'show'},
  :before=> "Element.show('loader')",
  :success => "Element.hide('loader')" )}"}%>

  </div></div>

  <div id="subjects"></div>
  <div id="modal-box" style="display:none;"></div>

  <div class="extender"></div>
  </div></div>
  #show.rjs
  if @question_type.empty?
   page.replace_html 'subjects', :partial => 'question_bank'
  else
   page.replace_html 'subjects', :partial => 'question_bank'
  end
  #_question_bank.html.erb
  <% unless @question_type.nil? %>
   <div class="add_sub">
   <%= link_to_remote 'Add Multiple Question ', :url => { :action =>   'new_multiple_choice', :id => @subject_id } %>
   </div>
   <div class="add_sub">
    <%= link_to_remote 'Add Fill Up Question', :url => { :action => 'new_fill_ups', :id => @subject_id } %>
   </div>
   <div class="add_sub">
   <%= link_to_remote 'Add Match Question', :url => { :action => 'new_matches', :id => @subject_id } %>
   </div>
   <div class="add_sub">
   <%= link_to_remote 'Add Descriptive Question', :url => { :action => 'new_descriptive', :id => @subject_id } %>
   </div>
   <% end %>
  #new_multiple_choice.rjs
  page.replace_html 'modal-box', :partial => 'new_multiple_choice'
  page << "Modalbox.show($('modal-box'), {title: 'Add new multiple choice questions', width: 500});"
   #_new_multiple_choice.html.erb

   <div id="grading-levels-form">
   <h4><%= @subject.name unless @subject.nil? %></h4>

   <% form_remote_for @quiz do |f| %>

    <% subject_id = (@subject.nil? ? nil : @subject.id) %>
    <% batch_id = (@subject.nil? ? nil : @subject.batch_id ) %>
    <%= f.hidden_field :batch_id, :value => batch_id %>
    <%= f.hidden_field :subject_id, :value => elective_group_id %>

     <div id="form-errors"></div>

     <div class="label-field-pair">
     <div class="label-container"><%= f.label :question %></div>
     <div class="input-container"> <%= f.text_field :question %></div>
     </div>
     <div class="label-field-pair">
     <div class="label-container"><%= f.label :answer1 %></div>
     <div class="input-container"> <%= f.text_field :answer1 %></div>
     </div>
     <div class="label-field-pair">
     <div class="label-container"><%= f.label :answer2 %></div>
     <div class="input-container"> <%= f.text_field :answer2 %></div>
     </div>
     <div class="label-field-pair">
     <div class="label-container"><%= f.label :answer3 %></div>
     <div class="input-container"> <%= f.text_field :answer3 %></div>
     </div>
     <div class="label-field-pair">
     <div class="label-container"><%= f.label :answer4 %></div>
     <div class="input-container"> <%= f.text_field :answer4 %></div>
     </div>

     <div class="label-field-pair">
     <div class="label-container"> <%= f.label :correct_option %></div>
     <div class="input-container"><%= f.select (:correct_option,%w{1,2,3,4}) %></div>
     </div>



     <%= f.submit "", :value => "► Save", :class => "submit_button" %>

     <% end %>
     </div>
     #server log
     Processing OnlineExamController#show (for 127.0.0.1 at 2011-04-05 19:31:00) [POST]
     Parameters: {"action"=>"show",      "authenticity_token"=>"OXozxkL1y+gCKkWxsUbAR8nH9Cb8MJJJPtnEVw8Y1AI=", "controller"=>"online_exam"}
     User Columns (0.8ms) SHOW FIELDS FROM `users`
     User Load (0.1ms) SELECT * FROM `users` WHERE (`users`.`id` = 8)
     CACHE (0.0ms) SELECT * FROM `users` WHERE (`users`.`id` = 8)
      Rendering online_exam/show
      Rendered online_exam/_question_bank (5.6ms)
      Completed in 21ms (View: 8, DB: 3) | 200 OK [http://localhost/online_exam/show]
       SQL (4.7ms) SET SQL_AUTO_IS_NULL=0


      Processing OnlineExamController#show (for 127.0.0.1 at 2011-04-05 19:31:02)      [POST]
       Parameters: {"action"=>"show",   "authenticity_token"=>"OXozxkL1y+gCKkWxsUbAR8nH9Cb8MJJJPtnEVw8Y1AI=", "controller"=>"online_exam"}
     User Columns (1.0ms) SHOW FIELDS FROM `users`
     User Load (0.8ms) SELECT * FROM `users` WHERE (`users`.`id` = 8)
     CACHE (0.0ms) SELECT * FROM `users` WHERE (`users`.`id` = 8)
     Rendering online_exam/show
     Rendered online_exam/_question_bank (5.6ms)
     Processing OnlineExamController#new_multiple_choice (for 127.0.0.1 at 2011-04-05 19:31:04) [POST]
     Parameters: {"action"=>"new_multiple_choice",   "authenticity_token"=>"OXozxkL1y+gCKkWxsUbAR8nH9Cb8MJJJPtnEVw8Y1AI=",  "controller"=>"online_exam"}
     User Columns (1.1ms) SHOW FIELDS FROM `users`
     User Load (0.1ms) SELECT * FROM `users` WHERE (`users`.`id` = 8)
     CACHE (0.0ms) SELECT * FROM `users` WHERE (`users`.`id` = 8)
       MultipleChoiceQuestion Columns (1.0ms) SHOW FIELDS FROM     `multiple_choice_questions`
     Completed in 22ms (View: 2, DB: 2) | 200 OK [http://localhost/online_exam/new_multiple_choice]
       SQL (0.1ms) SET SQL_AUTO_IS_NULL=0 here

我知道代码很长,几个小时都没关系了,当我点击最后的第一个链接时,我的问题_new_multiple_choice.html.erb没有呈现。

1 个答案:

答案 0 :(得分:6)

大多数Rubyist会被这段代码吓坏,然后逃跑。而不是回答你的问题(如果解决了,会使你的应用程序更加可怕),这里有一些基础知识;做这些,这样你就可以开始提出人们会回答的问题了。

Rails为您完成大部分工作,因此您无需查找与ID的关系或自行设置数组。实例变量用于将事物传递给视图。尝试准确地传递视图所需的内容,而不是更多。

首先,建立与模型的关系......

<强>模型/ user.rb

class User
  has_one :employee
end

<强>模型/ employee.rb

class Employee
  belongs_to :user
  has_and_belongs_to_many :employee_subjects
end

<强>模型/ employee_subject.rb

class EmployeeSubject
  has_and_belongs_to_many :employees
  has_one :batch
end

<强>模型/ batch.rb

class Batch
  belongs_to :employee_subject
end

has_and_belongs_to_many有点棘手,所以请仔细阅读文档: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many

然后,您可以将控制器操作简化为一行......

<强>控制器/ online_exams_controller.rb

def index
  @subjects = current_user.employee.employee_subjects
end

并在您的观点中......

<强>视图/ online_exams / index.html.erb

<ul>
<%= render :partial => '/subjects/list_view', :collection => @subjects, :as => :subject %>
</ul>

<强>视图/ employee_subjects / _list_view.html.erb

<li>
  Name: <%= subject.name -%>, Code: <%= subject.code -%>,
  Batch Name: <%= subject.batch.name -%>
</li>

在进行更多操作之前,请先考虑应用程序的对象应如何组织和相关,并尝试使用有意义的名称。阅读有关面向对象编程概念的更多信息,并遵循一些Rails教程,并习惯使代码行数减少10倍的应用程序!