2011-04-05 66 views
-1
#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][email protected]_name[@count].name+" "[email protected]_details[@count].name 
    @display_details[@count][1][email protected]_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不渲染当我点击末尾的第一个链接。Rails的不渲染部分(_new_multiple_choice.rjs)

+4

您需要改进您的代码。这不是Railsy或Rubyish。或漂亮。 – Zabba 2011-04-05 20:09:27

+0

@Zabba:我正在学习铁轨,如果我能知道我应该在哪里以及应该改进什么,那将会很好。 – Kracekumar 2011-04-06 01:46:59

+0

我怀疑你需要在*控制器*中使用如此多的实例变量(例如索引操作)。它也似乎是你的模型设置不正确。你是在做':select =>'name,code,batch_id''是否有很好的理由?在我看来,你正在尝试学习来自另一个背景(如ASP或Java)的Rails/Ruby?这有助于在Rails上获取尽可能多的书籍。 – Zabba 2011-04-06 02:26:54

回答

6

大多数Rubyists会被这段代码吓倒并逃跑。而不是回答你的问题(如果解决了这个问题,会让你的应用更加可怕),下面是一些基础知识;做这些,这样你就可以开始提问人们会回答的问题。

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

然后你就可以简化您的控制器行动,以一个行.. 。

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

Bef尽管如此,请考虑应如何组织和关联应用程序的对象,并尝试使用有意义的名称。阅读更多关于面向对象的编程概念,并遵循一些Rails教程,并使用少10倍的代码行来制作应用程序!