Solved

radio buttons to display list of options  - rails app

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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