html - Passing an instance attribute and association method as parameters in a ruby method -
i've written rails helper part of creating data-attributes options within select box.
helper
def list_attributed_options(instance_vars, attr, assoc_method) instance_vars.map { |instance_var| [instance_var.attr, instance_var.id, { :"data-#{assoc_method}" => instance_var.assoc_method.downcase.gsub(/\s+/, "-")}] } end
view code
<%= f.input :game_id, as: :select, collection: list_attributed_options(@games, "title", "console"), include_blank: "select game" %>
instance_vars
represents instance variables used collection (@games
), attr
represents 1 of instance variables' attributes , assoc_method
model belongs to.
instance_var.title = "halo" instance_var.id = 21 instance_var.console = "xbox one"
so example in particular case want name options game's title, name data attribute after console belonging game:
<select> <option data-console="xbox-one">halo</option> <option data-console="ps4">ratchet-&-clank</option> <option data-console="xbox-one">tomb raider</option> </select>
interpolating attr
parameter works fine when comes attaching them instance variable when run problems.
if understood want, "title"
should "name"
instead, yes? in case, instance_var.attr
can't work since instance_var
doesn't have attr
; has name
. want instance_var.send(attr.to_sym)
.
Comments
Post a Comment