Solved

radio buttons to display list of options  - rails app

Posted on 2011-09-25
4
341 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
  • 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 500 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now