ruby-on-railsrubysimple-form-for

Rails: Two values for label_method (simple_form_for)


I want to create a new pm_workload. I have the following code-snippet

<%= f.input :project_id, :collection => PmProject.order('name'), :label_method => :name, :value_method => :id, :label => "Project", :include_blank => false %>

Is there an option to use two values for :label_method? e.g.
:label_method => :name << (PmProject.project_number)


Solution

  • It's not possible to have to attributes or field name inside the label method. But I have an alternate soluction for you resolve this problem.

    You are taking PmProject collections, So now create a instance level method insdie the PmProject model like as below.

    def name_of_method
      "#{name} #{project_number}"
    end
    

    Now to use the same on the view input of the simple form for use like that as below...

    <%= f.input :project_id, :collection => PmProject.order('name'), :label_method => :name_of_method, :value_method => :id, :label => "Project", :include_blank => false %>