Rails CRUD error
So my experience is in Java web services, but I am trying to make a transition to ROR.
I am using FlexImage to handle loading images and creating thumbnails. I followed the explorer and the CRUD behavior worked fine at one point. However, at some point, the CRUD behavior for one of my models (images) was broken.
The error code I am returning looks like this: ActiveRecord::RecordNotFound in ImagesController#show -- Couldn't find Image with ID=#<Image:0x4e2bd74>.
In other words, when I tell Rails about create / update / destroy, it confuses the object with the ID. This indicates that a routing problem may arise. I thought adding partial images might be a problem, but rolling back the changes won't fix it.
Following are the new controller show and update methods for the Images model:
# images_controller.rb
# ...
def new
@image = Image.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @image }
end
end
# ...
def show
@image = Image.find(params[:id])
respond_to do |format|
format.jpg # show.jpg.erb
format.html # show.html.erb
format.xml { render :xml => @image }
end
end
# ...
def create
@image = Image.new(params[:image])
if @image.save
redirect_to image_url(@image)
else
flash[:notice] = 'Your image did not pass validation.'
render :action => 'new'
end
end
# ...
Note that show () expects a matching ID of course. Here's new.html.erb to load a new image:
# new.html.erb [upload image]
<h1>New image</h1>
<% form_for @image, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<table><tr><td width="50%">
<p>
<%= f.label :filename %><br />
<%= f.text_field :filename %></p>
</td>
<td><p><b>Upload Image</b><br />
<%= f.file_field :image_file %><br />
or URL: <%= f.text_field :image_file_url %>
<%= f.hidden_field :image_file_temp %>
</td>
<td>
<b>Uploaded Image:</b><br />
<%= embedded_image_tag(@image.operate { |img| img.resize 100 }) if @image.has_image? %>
</td>
</tr>
</table>
<p>
<%= f.label :description %><br />
<%= f.text_area :description %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', images_path %>
The relevant part of the .rb routes looks like this:
# routes.rb [excerpt]
map.resources :images
map.image 'images/:action/:id.:format', :controller => 'images'
Also note that the new image does indeed load , and the error is pushed to the redirect to show (which expects a valid id in the [: id] parameters, not the object it is being served for whatever reason.)
Thanks for your help in advance, and please let me know if anything comes up with you.
a source to share
From looking at the code, it seems to me that the problem might be caused by the use image_url(@image)
in combination with a non-RESTful route image
.
You probably want to remove the line
map.image 'images/:action/:id.:format', :controller => 'images'
from routes.rb
.
Line
map.resources :images
should be enough to show all CRUD actions in your ImagesController
.
a source to share
My suggestion is to use ruby-debug and set a breakpoint right before the call to Image.find. Check the [: id] parameters and see what it really is.
More ghetto approach, put this before the call to Image.find
logger.info params[:id].class
and see what's in that variable. Is it possible that you have some kind of filter before manipulating it?
a source to share