0

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:

sl--> has two types i) mrgsl. ii) evesl.

so when sl (check_box) is chosen it then show two radio_buttons and either of two is selected. 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.

PallavSharma
  • 439
  • 2
  • 12
  • 21

1 Answers1

1

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 with mrgsl"
"ls with evesl"

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 ?

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". So, I was thinking about making "ls " a string-type data in your model. Then you can give it whatever value you want : "evesl", "mrgsl", "disabled" and, later, add some more without any difficulty. 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.

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.

Hope it helps !

Community
  • 1
  • 1
Er...
  • 526
  • 4
  • 10
  • what i basically need is, when user select sl(short leave) (check_box) then it show two radio_buttons (mrgsl and evesl) then user can select one of them as well. – PallavSharma May 17 '14 at 04:17