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

  • Home
  • SEARCH
  • 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 4000788
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:49:30+00:00 2026-05-20T07:49:30+00:00

Given three models: Document Asset AssetCategory A document accepts_nested_attributes_for assets (a document has_many assets),

  • 0

Given three models:

  1. Document
  2. Asset
  3. AssetCategory

A document accepts_nested_attributes_for assets (a document has_many assets), and an asset belongs_to an asset category.

I would like to display a field for an asset attribute for each asset category.

I am currently achieving this as follows. Controller:

def new
  @document = Document.new
  @asset_categories = AssetCategory.all
  @asset_categories.count.times { @document.assets.build }
end

View (this example uses the semantic_fields_for method provided by Formtastic, but this is just a thin wrapper around fields_for):

i=0
f.semantic_fields_for :assets do |asset_form|
  asset_form.input :attachment, :label => @asset_categories[i].name
  asset_form.input :asset_category, :as => :hidden, :value => @asset_categories[i].id
  i+=1
end

Is there a cleaner approach to this? I’m not so fond of the temporary variable i.

  • 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-05-20T07:49:31+00:00Added an answer on May 20, 2026 at 7:49 am

    In the controller:

    def new
      @document = Document.new
      AssetCategory.all.each do |ac|
        @document.assets.build :asset_category_id=>ac.id
      end
    end
    

    In the view:

    f.semantic_fields_for :assets do |af|
      af.input :attachment, :label=>af.object.asset_category.name
      af.input :asset_category_id, :as => :hidden, :value => af.object.asset_category.id
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an array like {one two, three four five}, how'd you calculate the total
I have three models class Collection < ActiveRecord::Base has_many :presentations has_many :galleries, :through =>
Given the following setup, I have three assemblies. Web (ASP.NET MVC 3.0 RC1) Models
Given three tables Dates(date aDate, doUse boolean), Days(rangeId int, day int, qty int) and
This is a purely theoretical question. Given three simple classes: class Base { }
Given the following simple example: List<string> list = new List<string>() { One, Two, Three,
Given a file tree - a directory with directories in it etc, how would
Given a pretty basic source tree structure like the following: trunk ------- QA |--------
Given the following inheritance tree, what would be the best way of implementing it
**models.py** class Category(models.Model): name = models.CharField(max_length=50) def __unicode__(self): return self.name class Gender(models.Model): name =

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.