Solved

radio buttons to display list of options  - rails app

Posted on 2011-09-25
4
345 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
String to Hex for UDP packet 1 953
Get Latest files 4 310
Create an application - what tools to use? 4 615
vagrant box not showing up on virtual box 9 760
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

810 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