Question

I'm using rails 3.2.

I have many to many type of models. Is there a way to set a "value" of a model to field_for.label?

Here's what I want to do.

Client model

class Client < ActiveRecord::Base
  attr_accessible :name, :renewal_month1, :renewal_month10, :renewal_month11, :renewal_month12, :renewal_month2, :renewal_month3, :renewal_month4, :renewal_month5, :renewal_month6, :renewal_month7, :renewal_month8, :renewal_month9, :sales_person_id, :usable, :user_id, :licenses_attributes

  has_many :licenses, :dependent => :destroy
  has_many :systems, :through => :licenses
  accepts_nested_attributes_for :licenses

end

License model

class License < ActiveRecord::Base
  attr_accessible :amount, :client_id, :system_id

  belongs_to :client
  belongs_to :system
  def system_name
    self.system.name
  end

end

System model

class System < ActiveRecord::Base
  attr_accessible :name, :sort

  has_many :clients

  has_many :licenses
  has_many :clients, :through => :licenses

end

In client controller I built license object for all system.

def new
  @client = Client.new
  @title = "New Client"

  System.all.each do |system|
    @client.licenses.build(:system_id => system.id)
  end

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @client }
  end
end

In _form.html.erb I use fieds_for for licenses

<%= f.fields_for :licenses do |ff| %>
<tr>
    <td><%= ff.label :system_id %></td>
    </td>
    <td> <%= ff.number_field :amount %>
    <%= ff.hidden_field :system_id %> 
    <%= ff.hidden_field :system_name %> 
    </td>
</tr>
<% end %>

Result I get is this

<tr>
    <td><label for="client_licenses_attributes_0_system_id">System</label></td>
    </td>
    <td> <input id="client_licenses_attributes_0_amount" name="client[licenses_attributes][0][amount]" type="number" value="10" />
    <input id="client_licenses_attributes_0_system_id" name="client[licenses_attributes][0][system_id]" type="hidden" value="1" /> 
    <input id="client_licenses_attributes_0_system_name" name="client[licenses_attributes][0][system_name]" type="hidden" value="SYSTEMNAME" /> 
    </td>
</tr>

I want the label to look like this.

    <td><label for="client_licenses_attributes_0_system_id">SYSTEMNAME</label></td>

SYSTEMNAME is the value of the model SYSTEM. I have a virtual attribute in LICENSE model defined as system_name. I was able to get SYSTEMNAME in hidden_field so I think models and controllers are fine. I just couldn't find out how to set the value of a model to label.

Était-ce utile?

La solution

Why can't you use the following?

<%= ff.label :system_name %>

I think the next code should work as well

<%= ff.label :amount, ff.object.system_name %>

I can't test this, but I hope it will generate

<label for="client_licenses_attributes_0_amount">SYSTEMNAME</label>

Note, that it creates a label for amount field, so that when the user click on it, amount field will be focused.

Autres conseils

Have you tried adding system_name to the label

<%= f.fields_for :licenses do |ff| %>
<tr>
    <td><%= ff.label :system_id, :system_name %></td>

    <td> <%= ff.number_field :amount %>
    <%= ff.hidden_field :system_id %> 
    <%= ff.hidden_field :system_name %> 
    </td>
</tr>
<% end %>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top