Search code examples
ruby-on-railsassociationshamlsimple-form

Rails Form Association Field Shows Object Instead of Name


I have a form that edits an object with an association, but the select menu shows the associated object instead of the user-friendly name (i.e. #<User:0x007fdcac6da278> instead of SeanPerryman). How can I set the value for that association box to be a value associated with that object?

= simple_form_for(@captain) do |f|
  = f.error_notification
  = f.association :user 
  = f.input :name
  = f.association :world
  = f.button :submit

Looks like it is doing it for the World association, but not for the User association.

enter image description here

I've tried adding a value tag:

= f.association :user, :value => @captain.user.username

But that doesn't change the display, and inspecting the select box has no mention of the value tag that I added.


Solution

  • You can use simple_form's label_method option, like this:

    f.association :user, label_method: :username
    

    See https://github.com/plataformatec/simple_form#associations