Acts_as_attachment and belongs_to:

Hi all. I’ve gotten the acts_as_attachment tutorial to work just fine.
However now i’m trying to do something that’s not covered and keep
running into a brick wall. I’m pretty sure this is a simple concept
that I’m just not familiar with yet.

I’d like to association an image with a entry from a different model. I
suppose in other words I’m asking how to update two models from one
controllers create action. Example:

class TrackImage < ActiveRecord::Base
belongs_to :track
acts_as_attachment :storage => :file_system, :max_size =>
300.kilobytes, :content_type => :image
validates_as_attachment
end

class Track < ActiveRecord::Base
has_many :track_images
end

class Admin::TracksController < ApplicationController

stuff omitted for brevity’s sake

def create
@track = Track.new(params[:track])
if @track.save
flash[:notice] = ‘Track was successfully created.’
@track_image = TrackImage.create! params[:track_image]
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end
end

new.rhtml

<%= form_tag({:action => ‘create’}, :multipart => true) %>
<%= render :partial => ‘form’ %>
<%= submit_tag “Create” %>
<%= end_form_tag %>
<%= link_to ‘Back’, :action => ‘list’ %>

_form.rhtml

stuff omitted for brevity’s sake

<%= file_field 'track', 'track_image' %>

The above code is producting this error:
undefined method `track_image=’ for #Track:0x2278e10

_form.rhtml

stuff omitted for brevity’s sake

<%= file_field 'track', 'track_image' %>

The above code is producting this error:
undefined method `track_image=’ for #Track:0x2278e10

You didn’t say specifically but I assume there is where you’re getting
the error? Is that correct? If so, it’s exactly as it says. There
doesn’t exist a track_image method on the Track class. Your
relationship indicates that you have many track_images. Therefore you
have many track_images, not a single track_image method. Try using a
file_field_tag instead or bind the form to the TrackImage class
instead.

Michael

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs