简体   繁体   English

如何将切换按钮的值存储在Rails中的ruby中的数据库中?

[英]How to store value of toggle button in database in ruby on rails?

I'm having a button which says whether the user is active user or not. 我有一个按钮,该按钮指示用户是否为活动用户。 How to store the value of the button database. 如何存储按钮数据库的值。 I mean, if I activate the user using the button click the value should be '1' in the database. 我的意思是,如果我通过单击按钮激活用户,则数据库中的值应为“ 1”。 Else it should be '0'. 否则应为“ 0”。

<%= f.button ( f.object.active? ? "Active" : "Inactive" ), class: 'btn btn-brand m-btn m-btn--air m-btn--custom', html: { margin: '0 auto' }, id: 'active_button', onclick: 'change()' %>

Here I'm trying to toggle between active and inactive. 在这里,我试图在活动和非活动之间切换。

<script>
  function change() {
    var toggle = document.getElementById("active_button");
    if (toggle.innerHTML == "Active") {
      toggle.innerHTML = "Inactive";
    } else {
      toggle.innerHTML = "Active";
    }
  }
</script>

Toggle Button works like a radio button. 切换按钮的工作方式类似于单选按钮。 So you have to create a column for that in database and save the value in database like any other field. 因此,您必须在数据库中为此创建一个列,并将该值保存在数据库中,就像其他任何字段一样。

I hope this answers your question. 我希望这回答了你的问题。

我建议您要么在数据库中添加一个布尔值列,要么使用状态机( 例如AASM )来管理状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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