List of Checkboxes

Hi,

Does anyone have a link or information on how to handle lists of checkboxes in rails?

I am working on a property site and each property has a list of features which are rendered as a list of checkboxes. I am trying to figure out how to handle which ones are checked and which not

and persist them back to the database.

Sorry for the noob question.

Keith

Cool, Didn’t know there was a checkbox helper.

Thanks for the answer.

Keith

Hi!

The check_box helper in your view and a call to update_attributes in your controller are all you need :slight_smile:

Could you please give me the example of using the checkbox helper?

I need to show a list of tags so that tags belonging to the page are checked. I'm bewildered with "method" parameter — it should be a string and I don't know how to write it if I want to call @page.includes?(topic).

Your sincerely, Damian/Three-eyed Fish

hi damian i recently did a similar thing, so here's maybe what you are looking for - it may not be the best or most elegant way - i didn't use checkbox helper, because i couldn't get it going properly - i'm still a newbie, but this works

in your view:   <% for section in @sections%>

<input type="checkbox" id= "<%section.id%>" name="section_ids" value="<%=section.id%>" <%if @content.sections.include? section %> checked="checked" <%end%>

<%=section.name%>

<% end%>

in controller (new or create): @content.sections= Section.find(@params[:section_ids]) if @params[:section_ids]

hope this helps

george

Try this plugin

http://www.agilewebdevelopment.com/plugins/multiple_select_helper

HTH, Michael

Thanks Michael,

Just tried it out and it does exactly what I was looking for and in good rails style it took about 5 minutes as well.

Keith

Thank you very much! This is an approach I'm using now, I just wanted to see whether I can write less code. I'm thinking of writing my own helper for it, but as I'm a newbie, I should find out how to do it.

Yours sincerely, Damian/Three-eyed Fish