简体   繁体   English

rails4,simple_form,check_box,radio_button,控制器

[英]rails4, simple_form, check_box, radio_button, controller

I have generated a model using scaffolding. 我已经使用脚手架生成了一个模型。

rails g scaffold attendance ename:string entertime:datetime sl:boolean mrgsl:boolean evesl:boolean halfday:boolean 

now i want to show this using simple_form, and i want to make these relationships between them: 现在我想使用simple_form来显示它,并且我想在它们之间建立这些关系:

sl--> has two types i) mrgsl. sl->有两种类型:i)mrgsl。 ii) evesl. ii)evesl。

so when sl (check_box) is chosen it then show two radio_buttons and either of two is selected. 因此,当选择sl(check_box)时,它会显示两个单选按钮并选择两个。 and halfday is also a check_box. 半天也是一个复选框。

<%= simple_form_for @attendance, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :ename %>
<%= f.input :entertime, as: :time %>
<%= f.input :sl, as: :check_box %>
<%= f.input :mrgsl, as: :radio_button %>
<%= f.input :evesl, as: :radio_button %>
<%= f.input :halfday, as: :check_box %>

  <div class="form-group">
    <div class='col-md-offset-2 col-md-10'>
      <%= f.submit nil, :class => 'btn btn-primary' %>
      <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                  attendances_path, :class => 'btn btn-default' %>
    </div>
  </div>
<% end %>

this is how im trying to do it but it doesnot work,. 这是即时通讯试图这样做的方式,但它没有用。

RuntimeError in Attendances#new

Showing /home/pallavsharma/Workspace/crm/app/views/attendances/_form.html.erb where line #4 raised:

No input found for check_box

1<%= simple_form_for @attendance, :html => { :class => 'form-horizontal' } do |f| %>
2<%= f.input :ename %>
3<%= f.input :entertime, as: :time %>
4<%= f.input :sl, as: :check_box %>
5<%= f.input :mrgsl, as: :radio_button %>
6<%= f.input :evesl, as: :radio_button %>
7<%= f.input :halfday, as: :check_box %>

what are the possible ways to do it. 有什么可能的方法来做到这一点。

First, if I understood correctly what you wrote, you may want to re-build your model to fit your logic better. 首先,如果我正确理解了您编写的内容,则可能需要重新构建模型以更好地适应您的逻辑。

You want to give the user three options : 您想给用户三个选择:

"no ls" “没有ls”
"ls with mrgsl" “ ls与mrgsl”
"ls with evesl" “与evesl的ls”

Your model, with these booleans, says they have 8. But you don't want them to pick mrgsl if they haven't picked ls and you don't want them to pick mrgsl AND evesl , am I right ? 您的模型,这些布尔说,他们有8,但你不想让他们来接mrgsl如果他们没有挑LS,你不想让他们来接mrgslevesl,对吗?

It would make more sense ( and make your code a lot easier to maintain ) if you don't have to re-migrate your database each time you want to add a new option to your "ls". 如果您不必每次都想向“ ls”中添加新选项时都不必重新迁移数据库,那将更有意义(并使您的代码更易于维护)。 So, I was thinking about making "ls " a string-type data in your model. 因此,我正在考虑在模型中使“ ls”成为字符串型数据。 Then you can give it whatever value you want : "evesl", "mrgsl", "disabled" and, later, add some more without any difficulty. 然后,您可以根据需要提供任意值:“ evesl”,“ mrgsl”,“ disabled”,然后添加更多内容而没有任何困难。 You can then handle this in your controller. 然后,您可以在控制器中进行处理。 You won't be doing 你不会做的

Attendance.where(mrgsl: true)

but

Attendance.where(ls: "msgrl")

Anyway, to answer your question :in order to do what you want ( make your radio button appear if the checkbox is checked ) you'll need to write some JavaScript. 无论如何,要回答您的问题:为了做您想做的事情(如果选中此复选框,则使您的单选按钮出现),您需要编写一些JavaScript。

You can find everything in this thread to make it work the way you want. 您可以在此线程中找到所有内容,以使其按所需的方式工作。

Finally, to use a bit of the simple_form power, you can make a "collection" for your radio buttons. 最后,要使用一点simple_form功能,您可以为单选按钮创建一个“集合”

Hope it helps ! 希望能帮助到你 !

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

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