<philk-+Ez3uMna1njjEzHDjyhgkVaTQe2KTcn/@public.gmane.org>
2005-Sep-27 00:05 UTC
check_box helper and array
Hello fellow Rails developers
I am stuck with the following problem:
I have a Project that has_many languages. All available languages are saved
in a table. I a view I want the user to select the languages the project
supports. In order to do that I iterate over all available languages using:
<% for language in Language.find(:all) %>
<%= check_box("language", language.id,
@project.has_language?(language.id) ?
{:checked => "checked"} : {}) %>
<% end %>
With Project declared like this:
class Project < ActiveRecord::Base
has_many :languages
def has_language?(id)
for language in languages
return true if language.id==id
end
false
end
end
And language:
class Language < ActiveRecord::Base
belongs_to :project
End
This rather standard issued I thought can be solved easier in rails.
Something like an "automatic" check if the project contains a language
from
the global list of languages.
So far this is working, but is there a more clever way to check the
languages the project contains?
Thanks in advance,
Phil