Sherlock_Stalker Sherlock_Stalker - 1 year ago 253
Ruby Question

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)

Answer Source

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}"

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 %>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download