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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:00:53+00:00 2026-06-11T17:00:53+00:00

In rails 3.2 I created a post controller. Each post can have a different

  • 0

In rails 3.2 I created a post controller. Each post can have a different number of paperclip attachments. To achieve this I created a assets model where each asset has a paperclip attachment. One post has_many assets and assets belong_to post.

Asset model

class Asset < ActiveRecord::Base
    belongs_to :post
    has_attached_file :photo, :styles => { :thumb => "200x200>" }
end

Post model

 class Post < ActiveRecord::Base
      attr_accessible :content, :title
      has_many :assets, :dependent => :destroy
      validates_associated :assets
      after_update :save_assets

 def new_asset_attributes=(asset_attributes) 
        asset_attributes.each do |attributes| 
            assets.build(attributes) 
        end 
    end
    def existing_asset_attributes=(asset_attributes) 
        assets.reject(&:new_record?).each do |asset| 
        attributes = asset_attributes[asset.id.to_s] 
        if attributes 
            asset.attributes = attributes 
            else 
                asset.delete(asset) 
            end 
        end 
    end

    def save_assets 
        assets.each do |asset| 
            asset.save(false) 
        end 
    end 
end

Posts helper

module PostsHelper
  def add_asset_link(name) 
    link_to_function name do |post| 
      post.insert_html :bottom, :assets, :partial => 'asset', :object => Asset.new 
    end 
  end
end

Form for post

<%= form_for @post, :html => { :multipart => true } do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
        <% @post.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
    <div id="assets">
        Attach a file or image<br />
        <%= render 'asset', :collection => @post.assets %> 
      </div> 
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Asset partial

<div class="asset"> 
    <% new_or_existing = asset.new_record? ? 'new' : 'existing' %>
    <% prefix = "post[#{new_or_existing}_asset_attributes][]" %>

    <% fields_for prefix, asset do |asset_form| -%> 
        <p> 
          Asset: <%= asset_form.file_field :photo %> 
          <%= link_to_function "remove", "$(this).up('.asset').remove()" %> 
        </p> 
  <% end -%> 
</div>

Most of the code is taken from here: https://gist.github.com/33011 and I understand this is a rails2 app, anyway I don’t understand what this error means:

undefined method `new_record?' for nil:NilClass
Extracted source (around line #2):

1: <div class="asset"> 
2:     <% new_or_existing = asset.new_record? ? 'new' : 'existing' %>
3:     <% prefix = "post[#{new_or_existing}_asset_attributes][]" %>
4:     
5:     <% fields_for prefix, asset do |asset_form| -%> 
  • 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-11T17:00:54+00:00Added an answer on June 11, 2026 at 5:00 pm

    Try changing this

     <div id="assets">
        Attach a file or image<br />
        <%= render 'asset', :collection => @post.assets %> 
      </div>
    

    to

    <div id="assets">
      Attach a file or image<br />
      <% @post.assests.each do |asset|%>
        <%= render 'asset', :asset => asset %> 
      <% end %>
    </div>
    

    Possible reason is when u calling

    <%= render 'asset', :collection => @post.assets %> 
    

    You are passing a collection to the rendered partial but you are no where using ur collection passed by the name collection in the partial. you are using a variable Assest which doesnt exist in scope of that particular rendered partial 🙂

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

Sidebar

Related Questions

Ever since I created this Rails 3.2.3 project to use MiniTest, I have been
I created a rails model by doing script/generate model Customer name:string address:string city:string state:string
I am running rails 3.2 I have created a nested form (requests > tags)
I have created a table in my Ruby on Rails application that I am
I have created a blog application using Ruby on Rails which includes the ability
I am new to rails so go easy. I have created a blog. I
Rails 2.3.3 Database and Model Schema User (id,name,email) posts (id,subject,message,user_id) How can I display
Using Ruby on Rails, I have created a blog. The blog has posts, and
I'm new to rails and having issues with scope. I have two classes, Post
I created a routing test rails3 app with one model 'User': rails new routing_test_app

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.