Question

Ok, here my Problem: i have a tabel in a radius-system. to enable a hotspot-user i need two different records in this table. the record has the fields user, attr, op, value first record: attribute has to be Password (this record is only for the password here) and value is the password itself. second record: attribute has to be something like Max-Allowed-Session an value is an integer in seconds. So i add this as nested attributes to my controller clients. In new i do that:

def new
@client = Client.new
2.times do
  radcheck = @client.radchecks.build
end`
respond to........
end`

Ok, in my view i have this at the moment:

f.fields for :radchecks do |rcbuilder|
<p><%= rcbuilder.label :username %><br /><%= rcbuilder.text_field :username %>
for all fields .....
end

In my controller i built two radchecks, so this part is shown two times. That is status, but i wanna have something like this in my website

<first occurence of that form>
<%= rcbuilder.hidden_field :attr, :value => "password" %>
<%= rcbuilder.hidden_field :value, :value => @generated_password %>
<end first occurence>
<second occurence of that form>
<%= rcbuilder.hidden_field :attr, :value => "Max-Allowed-Session" %>
<%= rcbuilder.label :value, 'Time in hours' %><%= rcbuilder.text_field :value %>
<end second occurence>

Somone has an idea to realize that. Maybe i have to write the indexed fields myself, but how can i achieve that? thanks for help....

Was it helpful?

Solution

Assuming you are using accepts_nested_attributes_for :radchecks in your Client model.

<% index=1 %>
f.fields for :radchecks do |rcbuilder|
  <%= render :partial=>'radcheck_fields', :locals=>{:rcbuilder=>rcbuilder, :index=>index}%>
<% index+=1 %>
end

Next create a partial named radcheck_fields.html.erb with the following code.

<% if index.eql?(1) %>
  <%= rcbuilder.hidden_field :attr, :value => "password" %>
  <%= rcbuilder.hidden_field :value, :value => @generated_password %>
<% else %>
  <%= rcbuilder.hidden_field :attr, :value => "Max-Allowed-Session" %>
  <%= rcbuilder.label :value, 'Time in hours' %><%= rcbuilder.text_field :value %>
<% end %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top