?
Solved

radio buttons to display list of options  - rails app

Posted on 2011-09-25
4
Medium Priority
?
371 Views
Last Modified: 2012-05-12
so i have this collection select to select a category of job:

<%= collection_select(:job, :category_id, Category.all, :id, :name, {:include_blank => '-select job type-'} ) %>

Open in new window


instead i want to show the categories as a list of radio buttons a user can select rather than a drop down list.
0
Comment
Question by:depassion
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 4

Expert Comment

by:kristinalim
ID: 36595086
Did you happen to mean check boxes?

If you take a look at the has_many method in the Rails API, a has_many association sets up Job#category_ids and Job#category_ids= for the job instance:
@job.category_ids # => [1, 2, 3]
@job.category_ids = [1] # => [1]

Open in new window

So in your view you could do:
<% Category.all.each do |category| %>
<li>
  <%= check_box_tag 'job[category_ids][]', category.id, f.object.category_ids.include?(category.id) %>
  <%= category.name %>
</li>
<% end %>

Open in new window

With check boxes, though, if no check box has been checked no data for that form name would be sent to the server. In your controller action, you would have to ensure the value passed for categories_id is an array:
# If no check box in the set has been checked
params[:job][:category_ids] # => nil

# Set to a blank array to catch these cases.
params[:job][:category_ids] ||= []
@job.update_attributes(params[:job])

Open in new window

0
 

Author Comment

by:depassion
ID: 36595097
i did actually mean radio button as in the category section here https://jobs.37signals.com/jobs/new, as only 1 category can be selected
0
 
LVL 4

Accepted Solution

by:
kristinalim earned 2000 total points
ID: 36595109
Sorry about that. Simpler then:
<% Category.all.each do |category| %>
<li>
  <%= f.radio_button :category_id, category.id %>
  <%= category.name %>
</li>
<% end %>

Open in new window

0
 

Author Closing Comment

by:depassion
ID: 36595116
thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question