dimanche 20 décembre 2015

unable to update searialised columns for checkboxes in rails?

i am having the following model.

class Shift < ActiveRecord::Base
 serialize :day_list_1
 serialize :day_list_2
end

and i am having the following form partial.this partial is same for new and edit views.

_form.html.erb:

<%= form_for(@shift, html: {:class => 'form-inline'}) do |f| %>

    <%= check_box_tag 'shift[week_off1_weeks][]', '1', @shift.week_off1_weeks.include?('1'), :class=>"form-control", :style=>"height:23px;width:50px;margin-top:-1px;"%>

    <%= check_box_tag 'shift[week_off1_weeks][]', '2', @shift.week_off1_weeks.include?('2') , :class=>"form-control", :style=>"height:23px;width:50px;margin-top:-1px;"%>

    <%= check_box_tag 'shift[week_off1_weeks][]', '3', @shift.week_off1_weeks.include?('3'), :class=>"form-control", :style=>"height:23px;width:50px;margin-top:-1px;" %>

    <%= check_box_tag 'shift[week_off1_weeks][]', '4', @shift.week_off1_weeks.include?('4'), :class=>"form-control", :style=>"height:23px;width:50px;margin-top:-1px;" %>

    <%= check_box_tag 'shift[week_off1_weeks][]', '5', @shift.week_off1_weeks.include?('5'), :class=>"form-control", :style=>"height:23px;width:50px;margin-top:-1px;" %>   

<% end %>

this is the code in new and edit views.

new.html.erb:
<%= render 'form' %>

edit.html.erb:
<%= render 'form' %>

this is the code in controller:

def new
    add_breadcrumb "Add Shifts", :new_shift_path
    @shift = Shift.new
    @weekoffarray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    @day_list_one = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    @day_list_two = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
  end

  # GET /shifts/1/edit
  def edit
    @weekoffarray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    @day_list_one = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    @day_list_two = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
  end

  # POST /shifts
  # POST /shifts.json
  def create
    @shift = @company.shifts.new(shift_params)

    respond_to do |format|
      if @shift.save
    format.html { redirect_to @shift, notice: 'Shift was successfully created.' }
    format.json { render :show, status: :created, location: @shift }
      else
    format.html { render :new }
    format.json { render json: @shift.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /shifts/1
  # PATCH/PUT /shifts/1.json
  def update
    respond_to do |format|
      if @shift.update(shift_params)
    format.html { redirect_to @shift, notice: 'Shift was successfully updated.' }
    format.json { render :show, status: :ok, location: @shift }
      else
    format.html { render :edit }
    format.json { render json: @shift.errors, status: :unprocessable_entity }
      end
    end
  end

  def shift_params
    params.require(:shift).permit!
  end

here the problem is i am unable to update these check boxes in edit page. i am able to create the new record by selecting these check boxes. and in edit page i am able to view the data in check box fields that is already existed in the database. the problem is when i update these check boxes and click on update button,the edited data is not getting saved. it is always showing the previous data only. the updation is not happening. i am using mysql database. and this serialised column for check boxes is a string datatype.




Aucun commentaire:

Enregistrer un commentaire