Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8428989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:12:35+00:00 2026-06-10T05:12:35+00:00

I currently have a Customer form that accepts_nested_attributes_for Features. These features could be any

  • 0

I currently have a Customer form that accepts_nested_attributes_for Features. These features could be any number of things, but to keep this simple, we will assume they are First Name, Last Name and Date of Birth (i’m aware of the issues with this being EAV – if you have comments on this, please see my other question on the subject).

In my controller, I have:

3.times {@customer.features.build}

This works fine, and builds 3 features, which save as expected. What I would like, however, is to specify the feature type for each built feature in the view. My view currently looks like:

<%= customer.fields_for :features do |features_builder| %>
  <%= render :partial => "features/fixed_feature", :locals => {:feature => features_builder, :fixed_feature_type => "First Name", :form_actions_visible => false} %>
<% end -%>

As one would expect, this creates 3 First Name fields. What I want is to be able to specify 3 different types of feature, like this:

<%= customer.fields_for :features do |features_builder| %>
  <%= render :partial => "features/fixed_feature", :locals => {:feature => features_builder, :fixed_feature_type => "First Name", :form_actions_visible => false} %>
  <%= render :partial => "features/fixed_feature", :locals => {:feature => features_builder, :fixed_feature_type => "Last Name", :form_actions_visible => false} %>
  <%= render :partial => "features/fixed_feature", :locals => {:feature => features_builder, :fixed_feature_type => "Date of Birth", :form_actions_visible => false} %>
<% end -%>

Clearly the code as written above does not work, as it is trying to create 3 fields for each instance. What it seems that I need is an index on the fields_for loop that will allow me to iterate through and add an if clause to each of the partials, so that it fires only once, and each one fires on a different instance of feature.

Can anyone indicate how best I could do this? After searching, I can not seem to find a way to get the index of the fields_for loop.

EDIT: Per the first comment, for clarity: I would like to have three Features rendered on the page, one for each of the features built by the controller. I would like each feature to be rendered differently (with a different local variable passed for :fixed_feature_type in the code above). I am looking for the best way to do this.

EDIT 2: I’m aware that another way to do this would be to dynamically build the nested attributes in the controller (instead of building 3 at once initially) – is there a way to do this?

EDIT 3: Having spent some time searching for a way to do this dynamically, I have come upon a helper method from a Railscast that looks like:

def link_to_add_fields(name, f, association)
  new_object = f.object.class.reflect_on_association(association).klass.new
  fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
    render(association.to_s.singularize + "_fields", :f => builder)
  end
  link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
end

It appears that I may be able to define a helper method like this that will allow me to add various feature fields – I’ll be happy to mark as the answer any suggestions on how to modify this to suit my current situation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:12:37+00:00Added an answer on June 10, 2026 at 5:12 am

    I was able to overcome this by moving the rendering of my partial to a custom helper, which allowed me to build an object dynamically, as required.

    EDIT: As requested, my code is as follows:

    I have added the following code to my application_helper.rb file:

    module ApplicationHelper
    
      def add_feature_fields(feature_types, object_form_builder, actions_visible)
        feature_types.length.times {object_form_builder.object.features.build}
        i = -1
        object_form_builder.fields_for :features do |features_builder|
          i = i + 1
          render :partial => "features/fixed_feature", :locals => {:feature => features_builder, :fixed_feature_type => feature_types[i], :form_actions_visible => actions_visible}
        end
      end
    end
    

    I am calling this helper like so in my view:

    <%= add_feature_fields(["First Name", "Last Name", "Date of Birth"], customer, false) %>
    

    This allows me to add as many features as required for the model (in this case, a person) by simply adding additional items to the array.

    Hope this helps!

    Edit: ‘features/_fixed_feature.html.erb’

    <%= feature.hidden_field :feature_type_id, :value => FeatureType.where(:value => fixed_feature_type).first.id %>
    <fieldset>
            <div class="control-group">
            <label class="control-label"><Value><%= fixed_feature_type %></label>
            <div class="controls">
                <%= feature.text_field :value, :class => 'input-large' %>
            </div>
        </div>
        <% if form_actions_visible == true %>
            <div class="form-actions">
                <%= content_tag :button, :type => :submit, :class => "btn btn-info" do -%>
                    Save Changes
                <% end -%>
                <%= content_tag :button, :type => :cancel, :class => "btn btn-warning" do -%>
                    Cancel
                <% end -%>
            </div>
        <% end %>
    </fieldset>
    

    Nothing particularly special about this, just takes the local variable passed in by the helper to render the labels etc. without requiring a “feature type” box and then a value box (it renders only a value box, hence the type is fixed).

    Per your comments, if you wanted to add features for different models, you would just call the helper again and change the “customer” part (which is the form builder – more commonly this is ‘f’ but I prefer more semantic naming).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have one project that currently contains multiple packages. These packages make up
I have a customer who is currently using Excel to do their staff planning.
I have a customer table where I'm currently storing: Last Name First Name Middle
Currently I have this code var App = Ember.Application.create(); App.user = Ember.Object.create({ people: customers
Currently have a drop down menu that is activated on a hover (from display:none
I currently have a XSLT 2.0 Stylesheet that I am trying to remove empty
I currently have a form with inputs and name attributes. I'm able to get
I am currently creating a customer application for a local company. I have a
currently i have the new item form for my application. i used mustacheJS for
Let's say I have a form in C# containing textboxes that correspond to fields

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.