Reputation: 745
How can I best work with collections for input in forms? I re-use them in several forms. i.e. where to store and how to use in a DRY way?
Examples
Thanks for helping out.
Upvotes: 0
Views: 41
Reputation: 11813
Basically, you want to share data among your views? The simplest solution would be to define helper methods in ApplicationHelper
.
module ApplicationHelper
def languages
[:NL, :EN]
end
def document_annotations(sender_id, document_type_id)
Annotation.order(:name).where(:sender => sender_id, :documenttype => document_type_id)
end
end
On the other note, it seems like your array might logically belong to one of your classes in your domain. If there is a class that can it belongs to then use constants:
class SomeModelProbably
LANGS = [:NL, :EN]
end
# Then access it like this:
SomeModelProbably::LANGS
Also, that Annotation
filtering would probably be better rewritten as a scope:
class Annotation
scope :by_sender_and_doc_type, ->(doc) { order(:name).where(:sender => doc.sender_id, :documenttype => doc.document_type_id) }
end
# Then access it like this:
Annotation.by_sender_and_doc_type(@document)
Upvotes: 1