# Building a relationship between an image and a listing

**URL:** <https://www.ruby-forum.com/t/building-a-relationship-between-an-image-and-a-listing/225967>\
**Category:** Rails\
**Created:** [September 11, 2007, 4:40pm UTC](https://www.ruby-forum.com/t/building-a-relationship-between-an-image-and-a-listing/225967 "2007-09-11T16:40:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [September 11, 2007, 4:40pm UTC](https://www.ruby-forum.com/t/building-a-relationship-between-an-image-and-a-listing/225967/1 "2007-09-11T16:40:09Z")

</div>

[http://khamsouk.souvanlasy.com/2007/5/1/ajax-file-uploads-in-rails-using-attachment\_fu-and-responds\_to\_parent](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](http://www.w3.org/2001/tag/doc/whenToUseGet.html))  
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]

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [September 11, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/building-a-relationship-between-an-image-and-a-listing/225967/2 "2007-09-11T16:44:03Z")

</div>

to make it a little easier on the eyes: [Parked at Loopia](http://pastie.caboo.se/96092)

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [September 11, 2007, 5:24pm UTC](https://www.ruby-forum.com/t/building-a-relationship-between-an-image-and-a-listing/225967/3 "2007-09-11T17:24:05Z")

</div>

On Sep 11, 7:44 am, Dave B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## to make it a little easier on the eyes:[Parked at Loopia](http://pastie.caboo.se/96092)
> 
> Posted viahttp://www.ruby-forum.com/.

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

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [September 11, 2007, 7:22pm UTC](https://www.ruby-forum.com/t/building-a-relationship-between-an-image-and-a-listing/225967/4 "2007-09-11T19:22:13Z")

</div>

gene tani wrote:

> On Sep 11, 7:44 am, Dave B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > ## to make it a little easier on the eyes:[Parked at Loopia](http://pastie.caboo.se/96092)
> > 
> > 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](http://pastie.caboo.se/96155)

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