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 3666832
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:55:41+00:00 2026-05-19T01:55:41+00:00

I have a Content model which has one or many Audio files which need

  • 0

I have a Content model which has one or many Audio files which need to be added by the new/edit form.
What I have did is created the models with this relationship:

class Audio < ActiveRecord::Base
  belongs_to :content
  has_attached_file :audiofile,
end

class Content < ActiveRecord::Base
  ...
  has_many :audios
  accepts_nested_attributes_for :audios, :allow_destroy => true
end

Now in my new Content form I have the following:

<% f.fields_for :audios do |audiof| -%>

  <%= f.label :audiofile, 'Audio file:' %>&nbsp;
  <%= audiof.file_field :audiofile %>

<% end -%>

What I need it to do is show me the file_field only for a new Audio file and for the existing ones just print me a file size,name and probably a delete button.

I have also created a new record in the controller with:

@content.audios.build

I am using Rails 3.0.3 with Paperclip plugin for upload. Sorry if the question is too nooby.

Thanks.

  • 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-19T01:55:41+00:00Added an answer on May 19, 2026 at 1:55 am

    From my memory, you will be able to access to the instance of the object within the fields_for statement.

    Try something like that :

    <% f.fields_for :audios do |audiof| -%>
      <% if audiof.object.new_record? %>
        <%= f.label :audiofile, 'Audio file:' %>&nbsp;
        <%= audiof.file_field :audiofile %>
      <% else %>
        <%= "Filename = #{audiof.object.audiofile.filename}" %>
        <%= "url = #{audiof.object.audiofile.url}" %>
      <% end %>
    <% end -%>
    

    If audiof.object returns nil(In that case, it is not the good name), check by displaying all the public methods <% = raise audiof.public_methods.inspect %>

    The object method should return an instance of an Audio class.

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

Sidebar

Related Questions

Say I have default content on the left side of my application, which is
Let's say I have the following model: class Contest: title = models.CharField( max_length =
I have a modal popup that initially shows some content but expands a div
I have a content query web part that queries by content type against a
I have some content that i have to render as a excel file in
I have a content management application in the root of my website, and I'm
I have some content from server using $.ajax() function. I try to replace some
I'm starting a Wordpress Blog that will have adult content on it, so I'll
I want to accomplish the following scenario in Drupal: You have 2 content-types. Lets
In silverlight 2.0. I have some content that i want to scroll vertically and

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.