vendredi 26 août 2022

Rails HAML View: checkbox is always false on add a new record

I'm working on a view which contains a textfield and a checkbox, for some reasons the checkbox value is always false during the create method. This is the code from the view:

  =form_for :student_loan, url: admin_students_loan_path do |f|
    =f.text_field :name, placeholder: "Full Name"
    =f.check_box :active_loan
    =f.label :active_loan
    =button_tag( :class => "btn") do
      Add               

This is the create method from the controller:

def create
  @student_loan = current.school.student_loans.create(student_loans_params)

  if @student_loan.valid?
    redirect_to admin_student_loans_path
  else
    flash[:errors] = @program_type.errors.full_messages
    redirect_to admin_student_loans_path
  end
end              

def student_loans_params
  params.require(:student_loan).permit(:name, :active_loan, :_destroy)
end    

If I run the edit view and change the checkbox's value from false to true (checked) this change is stored in the database, this is the code from the edit view:

=form_for @student_loan, :url => {:controller => :student_loans, action: :update} do |f|
  %tr
    %td=f.text_field :name, placeholder: "Full name"
    %td
      =f.check_box :active_loan
      =f.label :active_loan
      %br
      =f.submit

And this is the update method from the controller:

def update
  @student_loan = current.school.student_loans.find(params[:id])
  @student_loan.update(student_loans_params)

  redirect_to admin_program_types_path
end

So I've been hours trying to find the difference or the bug in the create workflow, I would like to request your help in these two questions:

  1. how can I be sure about the checkbox's value passed to the controller?
  2. do you see anything in my code that is avoiding to catch the real value from the checkbox control?

Thank you so much




Aucun commentaire:

Enregistrer un commentaire