samedi 18 juillet 2015

show previously selected values in checkbox

this is my aau_subject model

class AauSubject < ActiveRecord::Base
    belongs_to :subject_types
    validates_presence_of :subject_type_id
    validates_presence_of :aau_subject_name
    validates_presence_of :aau_credit_hours
    serialize :prerequisite, Array
end

this is my prerequisite_subjects action under aau_subjects controller

def prerequisite_subjects
    @aau_subjects =AauSubject.find(:all)
     if  request.post? 
       @aau_subject = AauSubject.find(params[:aau_subject][:aau_subject_id]) 
       @aau_subject.update_attribute(:prerequisite,params[:prerequisite])     
      flash[:notice] = "#{t('success')}"
    redirect_to :action => "prerequisite_subjects"
  end
end


 def list_subjects
   @aau_subject = AauSubject.find(params[:aau_subject_id])
   @aau_subjects =AauSubject.find(:all, :conditions => ["id != ?", @aau_subject])
   render :update do |page|
     page.replace_html 'list-category-batch', :partial => 'list_subjects'
   end
 end

The view of prerequisite_subject.html.erb

<% form_for :aau_subject ,:url =>{:action => 'prerequisite_subjects'} %>
  <div id="right-side-inputs">
    <div class="label-field-pair">
      <label for="student_course"><%= t('select_a_category') %><span class="necessary-field">*</span> : <%= image_tag("loader.gif",
          :align => "absmiddle",
          :border => 0,
          :id => "loader",
          :style =>"display: none;" ) %></label>
      <div  class="text-input-bg">
        <%= select :aau_subject, :aau_subject_id,
          @aau_subjects.map {|b| [b.aau_subject_name, b.id]},
          {:prompt => "#{t('select_a_category')}"},{:onChange => "#{remote_function(:url => {:action => "list_subjects"}, :with => "'aau_subject_id='+value",:before => "$('loader').show();",
          :success => "$('loader').hide();")}"}%></div></div>
    <div class="label-field-pair category_list" id="list-category-batch">

    </div>
    </div>
       <%=  submit_tag "► #{t('create_text')}",:class=>'submit_button', :disable_with => "► #{t('please_wait')}" %>

partial view of list_subjects.html.erb

 <div class="label-container">
        <label><%= t('select_a_prerequisite') %><span class="necessary-field">*</span>:
          <b class="sel-list">
            <%= link_to_function t('all'), "$$('input.category_select').each(function(checkbox) { checkbox.checked = true; });" %>,
            <%= link_to_function t('none'), "$$('input.category_select').each(function(checkbox) { checkbox.checked = false; });" %>
          </b>
        </label>
      </div>
      <div class="categories">

        <% @aau_subjects.each do |c| %>
          <div class="each_category">
            <%= check_box_tag "prerequisite[]", c.id, :class=>"category_select" %> <label><%= "#{c.aau_subject_name}  #{c.aau_subject_code} " %></label>
          </div>
        <% end %></div>

In the above code , the column prerequisite is storing an array of ids of aau_subject, example prerequisite["1","2","3"] . My question is for the first time I select the subjects (say in aau_subject_id = 1 needs to store 2 and 3 ids in its prerequisite column) in list of check box and ill update the subject id in prerequisite column, when i want to update at second time for the same id (1) the check box should automatically show previously selected ids. how can i fetch the ids in prerequisite which is stored in array. Any help is valuable , Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire