yes i''m running on windows at the moment.
well i got the rmagick to load and put the
required "Rmagick" in the environment.rb
but when i try to post something with image it rolls back and does not
post. i followed the guide from the advanced rails recipes and just
changed the names around but bleh.
the view:
<% form_for(@item, :html => { :multipart => true }) do |f| %>
<%= error_messages_for :item, :image %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_area :description %>
</p>
<p>
<%= label :item, :image %>
<%= file_field_tag :image_file %>
<span class="hint">
We accept JPEG, GIF, or PNG files up to 500 KB.
</span>
</p>
<p>
<%= f.label :price %><br />
<%= f.text_field :price %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
controller
def new
@item = Item.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @item }
end
end
# GET /items/1/edit
def edit
@item = Item.find(params[:id])
end
# POST /items
# POST /items.xml
def create
@item = Item.new(params[:item])
@image = Image.new(:uploaded_data => params[:image_file])
@service = ItemService.new(@item, @image)
respond_to do |format|
if @service.save
flash[:notice] = ''Item was successfully created.''
format.html { redirect_to(@item) }
format.xml { render :xml => @item,
:status => :created,
:location => @item }
else
format.html { render :action => "new" }
format.xml { render :xml => @item.errors,
:status => :unprocessable_entity }
end
end
items_helper.rb
module ItemsHelper
def image_for(item, size = :medium)
if item.image
image_image = item.image.public_filename(size)
link_to image_tag(image_image), item.image.public_filename
else
image_tag("blank-image-#{size}.png" )
end
end
end
item_service.rb
class ItemService
attr_reader :item, :image
def initialize(item, image)
@item = item
@image = image
end
def save
return false unless valid?
begin
Image.transaction do
if @image.new_record?
@item.image.destroy if @item.cover
@image.item = @item
@image.save!
end
@item.save!
true
end
rescue
false
end
end
def valid?
@item.valid? && @image.valid?
end
def update_attributes(item_attributes, image_file)
@item.attributes = item_attributes
unless image_file.blank?
@image = Image.new(:uploaded_data => image_file)
end
save
end
end
development.log
Processing ItemsController#create (for 127.0.0.1 at 2008-10-28 22:08:46)
[POST]
Session ID:
BAh7BzoMY3NyZl9pZCIlMDc3Y2M3ZGVjN2U5OWFhMGY5ZDUzMTQ1ZTRlYTc1
ZGEiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhh
c2h7AAY6CkB1c2VkewA=--4332f39dc3fdbc9fc0d4d317ca2c57f1481d87df
Parameters:
{"image_file"=>#<File:C:/Users/HYUKYO~1/AppData/Local/Temp/CGI6348-3>,
"commit"=>"Create",
"authenticity_token"=>"e798b2552991572d78d139f4268655e69369ee32",
"action"=>"create",
"controller"=>"items",
"item"=>{"title"=>"adf",
"price"=>"23",
"description"=>"asdfasdf"}}
[4;35;1mItem Columns (0.004000) [0m [0mSHOW FIELDS FROM
`items` [0m
[4;36;1mImage Columns (0.004000) [0m [0;1mSHOW FIELDS FROM
`images` [0m
[4;35;1mSQL (0.001000) [0m [0mBEGIN [0m
[4;36;1mSQL (0.000000) [0m [0;1mROLLBACK [0m
Rendering template within layouts/items
Rendering items/new
Completed in 0.23000 (4 reqs/sec) | Rendering: 0.00400 (1%) | DB:
0.00900 (3%) | 200 OK [http://localhost/items]
--
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
-~----------~----~----~----~------~----~------~--~---