1

I have an Attendance model that has two Boolean database attributes (:late and :absent) and they both have :default => false.

I am trying to make a form that simply has 2 radio buttons. One for late and one for absent. Of course, the user can select only one of the two.

I've tried so many variations but can't get anything to work.

Can anyone lend me a hand? I know I could do collection select pretty easily but radio buttons will help the user experience. Also, I need to keep with the db schema for a bunch of reasons...

Thanks a million!!!

1
  • Are you saying you would like the user to be able to select both (i.e. set both to true)? Commented Oct 18, 2012 at 19:30

2 Answers 2

5

I am not aware of any rails helpers that will support the desired functionality out of the box. I also question the usability since a user will not be able to unselect either radio button if the model is neither late nor absent but gets clicked accidentally.

That aside, I'd use the radio_button_tag and provide two options, then in your controller update the params accordingly. Here's a basic example:

<%= form_tag('/your/path') do %>
  <%= radio_button_tag :status, 'late' %>
  <%= label_tag :status_late, 'Late' %>
  <%= radio_button_tag :status, 'absent' %>
  <%= label_tag :status_absent, 'Absent' %>
  <%= submit_tag :submit %>
<% end %>

Of course you'll have whatever other fields you need, and then in your controller something like:

def create
  case params[:status]
  when 'late'
    params[:model_name][:late] = true
  when 'absent'
    params[:model_name][:absent] = true
  end

  ModelName.new(params[:model_name])
end

Something along those lines should do the trick.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response! I think this is as good an answer as any. The general thought is that either the worker is absent or late. The model can be deleted if the user is marked either of the above inadvertently.
1
@options = ["Late", "Absent"]
@selected_options = ""

with

<% @options.each do |option| %>
    <%=  radio_button_tag 'attendance_radio', option, option == @selected_option %> <%= label_tag 'name', option %> <br />
<% end %>

which stores the selected radio in attendance_radio

If you want the value in the form to be something other than the actual selected value (late, absent) then you can mess with the second parameter to hold a boolean instead

3 Comments

^ This does not work for a bunch of reasons: 1) it creates two radio buttons that are unrelated - i.e., the user would be able to select both options, 2) the submitted values are late and absent, respectively and not the true/false as indicated in the question.
Edited answer. Sorry I answered on the fly.
Thanks a bunch for the edited response! I'll tinker around with your suggestion and try to get it to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.