I am working on a website that allows people who run a bed and breakfast business to post their rooms.
I would like to require them to include a “profile image” of posting when they publish it, but I also want to give them the opportunity to add more images later (this will be developed later).
I thought that the best thing that can be done is to use the Paperclip gem and have the “Accommodation and Photos” application in it, later owned by the former as an association.
A new photo is created when the object is created. It has id and accommodation_id attributes. However, the image never loads, and none of the Paperclip attributes are set (image_file_name: nil, image_content_type: nil, image_file_size: nil), so I get a “missing” Paperclip photo.
Any ideas on this? It held me back for several days.
Accommodation
models /accommodation.rb
class Accommodation < ActiveRecord::Base
validates_presence_of :title, :description, :photo, :thing, :location
attr_accessible :title, :description, :thing, :borough, :location, :spaces, :price
has_one :photo
end
Controllers / accommodation _controller.erb
class AccommodationsController < ApplicationController
before_filter :login_required, :only => {:new, :edit}
uses_tiny_mce ( :options => {
:theme => 'advanced',
:theme_advanced_toolbar_location => 'top',
:theme_advanced_toolbar_align => 'left',
:theme_advanced_buttons1 => 'bold,italic,underline,bullist,numlist,separator,undo,redo',
:theme_advanced_buttons2 => '',
:theme_advanced_buttons3 => ''
})
def index
@accommodations = Accommodation.all
end
def show
@accommodation = Accommodation.find(params[:id])
end
def new
@accommodation = Accommodation.new
end
def create
@accommodation = Accommodation.new(params[:accommodation])
@accommodation.photo = Photo.new(params[:photo])
@accommodation.user_id = current_user.id
if @accommodation.save
flash[:notice] = "Successfully created your accommodation."
render :action => 'show'
else
render :action => 'new'
end
end
def edit
@accommodation = Accommodation.find(params[:id])
end
def update
@accommodation = Accommodation.find(params[:id])
if @accommodation.update_attributes(params[:accommodation])
flash[:notice] = "Successfully updated accommodation."
render :action => 'show'
else
render :action => 'edit'
end
end
def destroy
@accommodation = Accommodation.find(params[:id])
@accommodation.destroy
flash[:notice] = "Successfully destroyed accommodation."
redirect_to :inkeep
end
end
view / post / _form.html.erb
<%= form_for @accommodation, :html => {:multipart => true} do |f| %>
<%= f.error_messages %>
<p>
Title<br />
<%= f.text_field :title, :size => 60 %>
</p>
<p>
Description<br />
<%= f.text_area :description, :rows => 17, :cols => 75, :class => "mceEditor" %>
</p>
<p>
Photo<br />
<%= f.file_field :photo %>
</p>
[... snip ...]
<p><%= f.submit %></p>
<% end %>
Photo
The controller and views are the same as when creating Rails.
models /photo.erb
class Photo < ActiveRecord::Base
attr_accessible :image_file_name, :image_content_type, :image_file_size
belongs_to :accommodation
has_attached_file :image,
:styles => {
:thumb=> "100x100#",
:small => "150x150>" }
end