Building a relationship between an image and a listing

http://khamsouk.souvanlasy.com/2007/5/1/ajax-file-uploads-in-rails-using-attachment_fu-and-responds_to_parent

I followed this tutorial, and would like to implement it into my
restaurant listing creation form. I am having troubles and am kind of
stuck, please guide me?

Here’s my MVC

_form.rhtml

[code=]<%= error_messages_for ‘restaurant’ %>

Name
<%= text_field 'restaurant', 'name' %>

Address
<%= text_field 'restaurant', 'location' %>

Telephone number (incl area code)
<%= text_field 'restaurant', 'contact' , 'size' => 10 , 'maxlength' => 10 %>

Category:
<%= select("restaurant", "category_id", Category.find(:all).collect {|c| [c.name, c.id] }) %>

Keywords used for the live search (chicken, seafood, BBQ, etc.)
<%= text_area "restaurant", "description" %>

Featured Listing?
<%=check_box 'restaurant','featured' %> <%= observe_field (:restaurant_featured, :function => "Effect.toggle('url', 'appear', {queue: {position: 'end', scope: 'featuredscope', limit: 2} })") %>

>

Url
<%= text_field 'restaurant', 'url' %>

Accepts Credit Cards?
<%= options_for_select([["Yes", "y"], ["No", "n"], ["Not Available", "x"]]) %>

Accepts Reservations?
<%= options_for_select([["Yes", "y"], ["No", "n"], ["Not Available", "x"]]) %>

Allow Children?
<%= options_for_select([["Yes", "y"], ["No", "n"], ["Not Available", "x"]]) %>

Don't Show On The Front Page?
<%= check_box 'restaurant', 'fpage' %>

<%= javascript_include_tag :defaults %>

Listing assets

    <% @assets.each do |asset| %> <%= render(:partial => '/assets/list_item', :object => asset)%> <% end %>

<% form_for(:asset, :url =>formatted_assets_path(:format => ‘js’), :html
=> { :multipart => true, :target => ‘upload_frame’}) do |form| %>
<%= render(:partial => ‘/assets/form’, :object => form) %>
<% end %>

Created at
<%= @restaurant.created_at %>

Updated at
<%= @restaurant.updated_at %>

[/code] admin_controller.rb [code=]class AdminController < ApplicationController before_filter :login_required

def index
list
render :action => ‘list’

end

GETs should be safe (see

URIs, Addressability, and the use of HTTP GET and POST)
verify :method => :post, :only => [ :destroy, :create, :update ],
:redirect_to => { :action => :list }

def list
@restaurant_pages, @restaurants = paginate :restaurants, :per_page
=> 100

end

def show
@restaurant = Restaurant.find(params[:id])
end

def new
@restaurant = Restaurant.new
@categories = Category.find(:all)
@asset = Asset.new(params[:asset])
end

def create
@restaurant = Restaurant.new(params[:restaurant])
@restaurant.add_picture(params[:asset])
if @restaurant.save
flash[:notice] = ‘Restaurant was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
@asset = Asset.new(params[:asset])
respond_to do |format|
if @asset.save
flash[:notice] = ‘Asset was successfully created.’
format.html { redirect_to asset_url(@asset) }
format.xml { head :created, :location => asset_url(@asset) }
format.js do
responds_to_parent do
render :update do |page|
page.insert_html :bottom, “assets”, :partial =>
‘assets/list_item’, :object => @asset
page.visual_effect :highlight, “asset_#{@asset.id}”
end
end
end
else
format.html { render :action => “new” }
format.xml { render :xml => @asset.errors.to_xml }
format.js do
responds_to_parent do
render :update do |page|
# update the page with an error message
end
end
end
end
end
end

def edit
@restaurant = Restaurant.find(params[:id])
@categories = Category.find(:all)
# use has_picture?
@asset = Asset.find(params[:restaurant_id])

end

def update
@restaurant = Restaurant.find(params[:id])
@restaurant.add_picture(params[:asset])
if @restaurant.update_attributes(params[:restaurant])
flash[:notice] = ‘Restaurant was successfully updated.’
redirect_to :action => ‘show’, :id => @restaurant
else
render :action => ‘edit’
end
@asset = Asset.find(params[:id])

respond_to do |format|

if @asset.update_attributes(params[:asset])
flash[:notice] = ‘Asset was successfully updated.’
format.html { redirect_to asset_url(@asset) }
format.xml { head :ok }
else
format.html { render :action => “edit” }
format.xml { render :xml => @asset.errors.to_xml }
end
end
end

def destroy
Restaurant.find(params[:id]).destroy
@asset = Asset.find(params[:id])
@asset.destroy

respond_to do |format|
  format.html { redirect_to assets_url }
  format.xml  { head :ok }
  format.js
end
redirect_to :action => 'list'

end
end[/code]
asset.rb(picture model)

[code=]class Asset < ActiveRecord::Base
belongs_to :restaurant
has_attachment :storage => :file_system,
:max_size => 1.megabytes,
:resize_to => ‘320x200>’,
:processor => :MiniMagick, # attachment_fu looks in
this order: ImageScience, Rmagick, MiniMagick
:content_type => :image

validates_as_attachment # ok two lines if you want to do validation,
and why wouldn’t you?
end[/code]
restaurant.rb

[code=]class Restaurant < ActiveRecord::Base

def has_picture?
self.asset.nil? ? false : true
end
def add_picture(picture = nil)
return if asset.nil? or picture[:uploaded_data]== “”
@pic = Picture.new(picture)
self.picture = @pic
end
belongs_to :category
has_one :asset
before_validation do |restaurant|
%w(image_path contact).each do |attribute|
restaurant[attribute] = nil if restaurant[attribute].blank?
end
end
validates_presence_of :name, :category, :location
validates_uniqueness_of :name

validates_format_of :contact,
:with =>
/^([a-zA-Z][\w.-][a-zA-Z0-9]@[a-zA-Z0-9][\w.-][a-zA-Z0-9].[a-zA-Z][a-zA-Z.]*[a-zA-Z]|[1]?[-|\s|.]?(?[0-9]{3})?[-|\s|.]?[0-9]{3}[-|\s|.]?[0-9]{4})$/,
:message => “is not a valid phone number (incl. area code)”,
:allow_nil => true

end[/code]

to make it a little easier on the eyes: Parked at Loopia

On Sep 11, 7:44 am, Dave B. [email protected]
wrote:

to make it a little easier on the eyes:Parked at Loopia

Posted viahttp://www.ruby-forum.com/.

maybe you could state your problem, or ask a question.

gene tani wrote:

On Sep 11, 7:44 am, Dave B. [email protected]
wrote:

to make it a little easier on the eyes:Parked at Loopia

Posted viahttp://www.ruby-forum.com/.

maybe you could state your problem, or ask a question.

Well when I go to make a new listing, I get this error.
http://pastie.caboo.se/96155

When I go to edit a listing, I get this error.
http://pastie.caboo.se/96157