Rails - 我需要一个复选框来更改DB中的字段 [英] Rails - I need a checkbox to change a field in the DB

查看:120
本文介绍了Rails - 我需要一个复选框来更改DB中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我以前做过,但对于我的生活,我无法想象出来。



我有一个表格,在里面。我需要使用复选框来更新数据库,并检查被调用是否为true。不需要是AJAX,只需更新字段。



表:rsvp
字段:调用



感谢一吨。

解决方案

没有ajax的简单方法可以使用表单内的复选框,








$ b b

查看

 <%form_for @rsvp,:id => rsvpdo | f | %> 
<%= f.check_box:called,:onclick => $('#rsvp')。submit()%>
<%end%>

这个如果你使用JQuery ...原型的onclick字符串将是:

  $('rsvp')。submit()

控制器

  @rsvp = Rsvp.find [:id])
if @ rsvp.update_attributes(params [:rsvp])
#success
else
#fail
end






参考:



复选框


I know I have done this before, but for the life of me I can't figure it out.

I have a table with a "called" field in it. I need to use a checkbox to update the db and check if "called" is "true" or not. Doesn't need to be AJAX, just needs to update the field.

table: rsvp field: called

Thanks a ton.

解决方案

A simple approach without ajax could be using a checkbox inside a form and submitting the form with the checkbox javascript onclick event.

Example:


View:

<% form_for @rsvp, :id => "rsvp" do |f| %>
  <%= f.check_box :called, :onclick => "$('#rsvp').submit()" %>
<% end %>

this if you are using JQuery... with prototype the onclick string will be:

$('rsvp').submit()

Controller:

@rsvp = Rsvp.find(params[:id])
if @rsvp.update_attributes(params[:rsvp])
  # success
else 
  # fail
end


Reference:

check box

这篇关于Rails - 我需要一个复选框来更改DB中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆