简体   繁体   中英

How to get information from a checkbox back into the DB in Ruby

I'm pretty sure I'm being an idiot here, but I've been out of Ruby long enough that my searching isn't coming up with the right answer.

I have a popup with a checkbox. I want, if the user checks the checkbox, to set a flag in the Users table so that the checkbox doesn't come up again.

I already have the code for if the thing is set, the popup doesn't come up. I'm having trouble getting the checkbox state-change back to the DB...

The checkbox code look like this:

    %button.btn.btn-primary.slide_show_next{:type => "button", :data => {:toggle => "modal", :target => "#help_slide_show_2", :dismiss => "modal"}}
      Next
      .show-slideshow
        %label
          %input.show-slideshow-checkbox{:type => "checkbox", :checked => "checked"}
          Show me this when I view a report.

The relevant coffeeScript is:

$ ->
  if typeof(gon) != 'undefined' && gon.show_help_slide_show == true && document.cookie.indexOf("show-slide-show=false") == -1
    $("#help_slide_show").modal()

  if document.cookie.indexOf("show-slide-show=false") != -1
    $(".show-slideshow-checkbox").attr("checked", false)

  $(".show-slideshow-checkbox").change( (event) ->
    val = $(event.target).prop("checked")

    document.cookie = "show-slide-show=#{val}; Path=/;"
    $(".show-slideshow-checkbox").attr("checked", val)
  )

Look into form_for . That will help you update the model from the view. The code for a haml form_for is below, and the erb equivalent can be found in the form_for documentation. I find that haml documentation isn't as robust as I'd like for certain syntax questions.

= form_for @user, remote: true do |f|
    = f.label 'checkbox'
    = f.check_box :table_column, autofocus: true
    = f.submit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM