所以我的背景是在Java Web服务,但我想转用ROR。

我使用FlexImage来处理图像上载和缩略图生成。我跟着引导和CRUD行为在一个点上工作的罚款。然而,在某些时候,我的模型(图像)的一个CRUD行为被打破。

在错误代码我取回如下:ActiveRecord::RecordNotFound in ImagesController#show -- Couldn't find Image with ID=#<Image:0x4e2bd74>.换句话说,当我告诉Rails创建/更新/破坏,它是混淆与所述ID的对象。这似乎表明有可能是一个路由问题。我想添加一个局部的图像可能会出现的麻烦,但回退更改没有解决它。

以下是控制器为图像模型的新,显示和更新方法:

      # 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 
     # ...

请注意显示(),当然,期望合适的ID。这里的new.html.erb上传新的图像:

# 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 %>

是routes.rb中的相关部分,如下所示:

# routes.rb [excerpt]

  map.resources :images
  map.image 'images/:action/:id.:format', :controller => 'images'

另外请注意,新的图像确实真正得到上传并引发该错误的重定向显示(这是预期在PARAMS有效ID [:编号],而不是对象,无论出于何种原因它正在切换。)

感谢您的帮助提前,并请让我知道如果有什么你跳了出来。

有帮助吗?

解决方案

从看代码看来,我认为该问题可能通过组合使用image_url(@image)与非RESTful image路线而引起的。

您可能会想删除行

map.image 'images/:action/:id.:format', :controller => 'images'

routes.rb

map.resources :images

实际上应该足以暴露在你的ImagesController所有CRUD操作。

其他提示

我的建议是使用红宝石调试和Image.find调用之前设置一个断点右。检查PARAMS [:编号],看看它究竟是什么。

一个更隔都方法中,Image.find呼叫之前把这个

logger.info params[:id].class

和所看到的是在该变量。有没有可能是你有某种过滤器之前被操纵呢?

  redirect_to :action => "show", :id => @image

我想这是编码重定向更习惯的方法。和+1到MOLF的意见,关于REST风格的路线。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top