-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]=@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

I know code is very long and been breaking my head for hours,My problem _new_multiple_choice.html.erb is not rendered when i click the first link at the end.

Kracekumar
  • 19,457
  • 10
  • 47
  • 56
  • 4
    You need to improve your code. That isn't Railsy or Rubyish. Or pretty. – Zabba Apr 05 '11 at 20:09
  • @Zabba:i am learning rails,it will be nice if i can know where and what i should improve. – Kracekumar Apr 06 '11 at 01:46
  • I doubt you need to use so many instance variables in the *controller* (eg index action). It also appears that your models are not set up correctly. Are you doing `:select=>'name,code,batch_id'` for good reason? It appears to me that you are attempting to learn Rails/Ruby coming from another background such as ASP or Java ? It would help to pick up as many books as possible on Rails. – Zabba Apr 06 '11 at 02:26
  • @zaba:i dont want to fetch all details only 3 details i need so i do :select =>'name,code,batch_id'.I come from PHP background – Kracekumar Apr 06 '11 at 19:07
  • I take it when you say you're learning rails, you're following a book or tutorials and not guessing your way along :O – mark May 06 '11 at 15:17
  • I would recommend moving the view contents into a [gist](gist.github.com), and the server log into another, separate gist. I also wonder if all of the view files included are necessary to solve your problem. Giving too much information just leaves the readers overloaded. I'd recommend isolating the code you think is problematic to make your question more answerable. I've also solved some of my own questions this way. – Eric Hu Sep 10 '11 at 05:45

1 Answers1

6

Most Rubyists would be horrified by this code and run away. Instead of answering your question (which, if solved, would make your app even more horrific), here are some basics; do these so you can start asking questions that people would answer.

Rails does most of the work for you so you don't have to find relationships with ids or set arrays up yourself. Instance variables are used to pass things on to the view. Try to pass the view exactly what it needs and no more.

First, set up your relationships with the models...

models/user.rb

class User
  has_one :employee
end

models/employee.rb

class Employee
  belongs_to :user
  has_and_belongs_to_many :employee_subjects
end

models/employee_subject.rb

class EmployeeSubject
  has_and_belongs_to_many :employees
  has_one :batch
end

models/batch.rb

class Batch
  belongs_to :employee_subject
end

has_and_belongs_to_many is a bit tricky, so read the docs carefully: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many

Then you can simplify your controller actions to one line...

controllers/online_exams_controller.rb

def index
  @subjects = current_user.employee.employee_subjects
end

and in your views...

views/online_exams/index.html.erb

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

views/employee_subjects/_list_view.html.erb

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

Before you do any more though, think through how your application's objects should be organised and related, and try to use names that make sense. Read more about object-oriented programming concepts, and follow some Rails tutorials, and get used making apps with 10 times fewer lines of code!

jimworm
  • 2,731
  • 18
  • 26