0
votes

I have a model called Listing. In my index.html.erb for listings. I have all the listings on the screen. The _form.html.erb for the Listing model look like this:

<%if user_signed_in? %>
<%=link_to "Sign Out",  destroy_user_session_path, :method => :delete %><br/>
<%= form_for(@listing, :html => {:multipart => true} ) do |f| %>
<% if @listing.errors.any? %>
 <div id="error_explanation">
   <h2><%= pluralize(@listing.errors.count, "error") %> prohibited this listing from   being saved:</h2>
  <ul>
  <% @listing.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
  </div>
 <% end %>
 <div class="field">
 <%= f.label :name %><br />
 <%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :id %><br />
<%= f.text_field :id %>
</div>

<%= f.label :image %>
<%= f.file_field :image %> <%= f.submit %> <% end %> <%else%> <%=link_to "Sign in", new_user_session_path %>
Contact the admin to obtain a login and password <%end%>

Here the model associated with this view is "Listing", but I would like to store the uploaded image attributes in a different model in the app: "Image".

Basically what Im trying to do is, make all my models upload their own photos through their views but save all the image attributes in a single model called "Image". And the images themselves are stored on Amazon S3.

So the question is what code goes in the "Listing" model to make this happen and what code goes in the "Image" model to make this happen. And how does this information get passed from the Listing to the Image?

Currently in the Listing model:

class Listing < ActiveRecord::Base

 has_attached_file :image,:styles => {:thumb => "75x75>", :small => "150x150>", :medium => "300x300>"}

end

Currently in the "Image"model

# == Schema Information
#
# Table name: images
#
#  id                 :integer         not null, primary key
#  image_file_name    :string(255)
#  image_content_type :string(255)
#  image_file_size    :integer
#  image_type         :string(255)
#  created_at         :datetime        not null
#  updated_at         :datetime        not null 
#

class Image < ActiveRecord::Base
end

Thanks in advance for your help.

2

2 Answers

1
votes

Just to be explicit, here's what you would have in your models:

Listing Model

class Listing < ActiveRecord::Base
  has_one :image, dependent => :destroy
end

Image Model

class Image < ActiveRecord::Base
  has_attached_file :image,
                    :styles => {:thumb => "75x75>", 
                                :small => "150x150>", 
                                :medium => "300x300>"}
end

Then in your form (from DVG's answer):

<% fields_for @listing.image do | img | %>
  <%= img.file_field :image %>
  #etc
  <% end %>
<% end %>
1
votes

I'm assuming a relationship between listing and image, but you get the idea

<% fields_for @listing.image do | img | %>
  <%= img.file_field :image %>
  #etc
  <% end %>
<% end %>

Of course, if you don't want them to be related, you can just make a new Image object and pass it to the field_for, but it makes sense to keep them related!