Hi,
I can’t understand the RoR-API concerning certain form helpers. Can you
maybe help me?
I have a model, Film, with a habtm-relationship to the model Language.
Now if I want to have a form for editing/creating a Film, I’d like to
have checkboxes for each Language with those checked that are associated
with the Film in question.
Is there an “easy” way to do this (with a helper method similar to
collection_select, maybe)? If not, could you point me in the right
direction?
- Do I need fields_for?
- What data do I feed the checkboxes?
- What is an elegant way to collect the data and insert it into the db?
Any pointers would be greatly appreciated.
A sleepless
Daniel
On 8/28/06, Daniel J. [email protected] wrote:
Is there an “easy” way to do this (with a helper method similar to
collection_select, maybe)? If not, could you point me in the right
direction?
That’s easy enough:
Controller:
def edit
@movie = Movie.find(params[:id])
@languages = Language.find(:all)
end
View:
Languages:
<% @languages .each do |lang| %>
<%= check_box_tag ‘movie[language_ids][]’, lang.id,
@movie.languages.include?(lang), :id=>“cb_#{lang.name}” %>
<%=lang.name%>
<% end %>
The trick is in the “movie[language_ids][]”. Nothing else to do, just
do the standard Movie.new(params[:movie]) stuff in your controller and
everything should be fine.
Cheers
Max
Max, you rock. I create forms like this all the time, and I kept
thinking
that there must be a better way - and lo and behold, there is.
Indeed you rock! Thanks for helping!