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'' %>
<!--[form:restaurant]-->
<p><label
for="restaurant_name">Name</label><br/>
<%= text_field ''restaurant'', ''name''
%></p>
<p><label
for="restaurant_location">Address</label><br/>
<%= text_field ''restaurant'', ''location''
%></p>
<p><label for="restaurant_contact">Telephone number (incl
area
code)</label><br/>
<%= text_field ''restaurant'', ''contact'' ,
''size'' => 10 , ''maxlength'' =>
10 %></p>
<p><label
for="restaurant_category_id">Category:</label><br/>
<%= select("restaurant", "category_id",
Category.find(:all).collect {|c|
[c.name, c.id] }) %></p>
<p><label for="restaurant_description">Keywords used for
the live search
(chicken, seafood, BBQ, etc.)</label><br/>
<%= text_area "restaurant", "description" %></p>
<p><label for="restaurant_featured">Featured
Listing?</label><br/>
<%=check_box ''restaurant'',''featured''
%>
<%= observe_field (:restaurant_featured,
:function => "Effect.toggle(''url'',
''appear'', {queue: {position: ''end'',
scope: ''featuredscope'', limit: 2} })") %>
<div id="url" <%=
"style=''display:none''" unless
@restaurant.featured==1
%>>
<p><label
for="restaurant_url">Url</label><br/>
<%= text_field ''restaurant'', ''url''
%></p>
<p><label for="restaurant_ccards">Accepts Credit
Cards?</label><br/>
<select name="restaurant[ccards]"><%=
options_for_select([["Yes", "y"],
["No", "n"], ["Not Available", "x"]])
%></select>
</p>
<p><label for="restaurant_reservations">Accepts
Reservations?</label><br/>
<select name="restaurant[reservations]"><%=
options_for_select([["Yes",
"y"], ["No", "n"], ["Not Available",
"x"]]) %></select>
</p>
<p><label for="restaurant_kids">Allow
Children?</label><br/>
<select name="restaurant[kids]"><%=
options_for_select([["Yes", "y"],
["No", "n"], ["Not Available", "x"]])
%></select>
</p>
<p><label for="restaurant_fpage">Don''t Show On
The Front
Page?</label><br/>
<%= check_box ''restaurant'', ''fpage''
%></p>
<%= javascript_include_tag :defaults %>
<h1>Listing assets</h1>
<ul id="assets">
<% @assets.each do |asset| %>
<%= render(:partial => ''/assets/list_item'', :object
=> asset)%>
<% end %>
</ul>
<br />
<% form_for(:asset, :url =>formatted_assets_path(:format =>
''js''), :html
=> { :multipart => true, :target => ''upload_frame''})
do |form| %>
<%= render(:partial => ''/assets/form'', :object =>
form) %>
<% end %>
<iframe id=''upload_frame'' name="upload_frame"
style="width:1px;height:1px;border:0px"
src="about:blank"></iframe>
</div>
<p><label for="restaurant_created_at">Created
at</label><br/>
<%= @restaurant.created_at %></p>
<p><label for="restaurant_updated_at">Updated
at</label><br/>
<%= @restaurant.updated_at %></p>
<!--[eoform:restaurant]-->[/code]
admin_controller.rb
[code=]class AdminController < ApplicationController
before_filter :login_required
def index
list
render :action => ''list''
end
# GETs should be safe (see
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]
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Dave Barnow
2007-Sep-11 14:44 UTC
Re: Building a relationship between an image and a listing
to make it a little easier on the eyes: http://pastie.caboo.se/96092 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gene tani
2007-Sep-11 15:17 UTC
Re: Building a relationship between an image and a listing
On Sep 11, 7:44 am, Dave Barnow <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> to make it a little easier on the eyes:http://pastie.caboo.se/96092 > -- > Posted viahttp://www.ruby-forum.com/.maybe you could state your problem, or ask a question. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dave Barnow
2007-Sep-11 17:22 UTC
Re: Building a relationship between an image and a listing
gene tani wrote:> On Sep 11, 7:44 am, Dave Barnow <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> to make it a little easier on the eyes: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 When I go to edit a listing, I get this error. http://pastie.caboo.se/96157 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---