CRUD ActionController :: RoutingError(没有路由匹配[GET]

时间:2014-09-08 23:50:28

标签: ruby-on-rails routes activeadmin

抱歉新手提醒。我是ROR的新手而不是网络开发人员。我很难用activeadmin理解路由。 我有一个退出的postgresql表,其中包含bytea列来存储jpeg图像和其他文本数据。

我遵循了本教程http://archive.railsforum.com/viewtopic.php?id=4642

这是迁移文件。

class CreateDrstests < ActiveRecord::Migration
  def change
    create_table "drstests", primary_key: "drstestid", force: true do |t|
      t.string   "serial",  limit: 10
      t.datetime "testdate"
      t.string   "result",     limit: 10
      t.string   "comment",    limit: 2000
      t.binary   "testimage"
    end
  end
end

将postgresql表中的所有数据显示为不带jpeg图像的列表。(工作)。使用jpeg图像显示单个测试结果。 (破碎)

app/admin/drstest.rb

    ActiveAdmin.register Drstest do

       index do
         column :testtype
         column :testdate
         column :result
         default_actions
       end

       def code_image
         @image_data = Drstest.find(params[:drstestid])
         @image = @image_data.testimage
         send_data(@image, :type     => imge/jpeg,
                     :filename => @image_data.testdate,
                     :disposition => 'inline')
       end

    end

views/drstests/show.html.erb

    <p id="notice"><%= notice %></p>
    <% for column in Drstest.content_columns %>
    <p>
    <b><%= column.human_name %>:</b>
    <% if column.name == "testimage" %>
      <%= image_tag("/admin/code_image/#{@drstest.drstestid }", :alt => "Image") %>
    <% else %>
      <%=h @drstest.send(column.name) %>
    <% end %>
    </p>
    <% end %>

     <%= link_to 'Back', drstests_path %>

这是development_log

中的错误
Started GET "/drstests/5" for 127.0.0.1 at 2014-09-08 18:11:07 -0400
Processing by DrstestsController#show as HTML
  Parameters: {"id"=>"5"}
  Drstest Load (0.5ms)  SELECT  "drstests".* FROM "drstests"  WHERE "drstests"."drstestid" = $1 LIMIT 1  [["drstestid", 5]]
  Rendered drstests/show.html.erb within layouts/application (1.8ms)
Completed 200 OK in 425ms (Views: 421.4ms | ActiveRecord: 0.5ms)


Started GET "/admin/code_image/5" for 127.0.0.1 at 2014-09-08 18:11:08 -0400

ActionController::RoutingError (No route matches [GET] "/admin/code_image/5"):
  actionpack (4.1.4) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.1.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.4) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.4) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.4) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.4) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.4) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.1.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.4) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.1.4) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.4) lib/rails/engine.rb:514:in `call'
  railties (4.1.4) lib/rails/application.rb:144:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  /usr/share/ruby/webrick/httpserver.rb:138:in `service'
  /usr/share/ruby/webrick/httpserver.rb:94:in `run'
  /usr/share/ruby/webrick/server.rb:295:in `block in start_thread'
  Rendered /usr/local/share/gems/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.8ms)
  Rendered /usr/local/share/gems/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/routes/_route.html.erb (4.6ms)
  Rendered /usr/local/share/gems/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.9ms)
  Rendered /usr/local/share/gems/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (49.8ms)

我是否需要在routes.rb中插入自定义路由?很确定查询是否有效。只是不明白为什么我无法显示图像。不知道如何调试问题。任何线索都会非常有帮助。谢谢您阅读此篇。

此致

1 个答案:

答案 0 :(得分:0)

要调试问题,您需要确定错误是什么,然后解决它:


错误

在任何调试过程中,您都必须从错误开始:

(No route matches [GET] "/admin/code_image/5")

此错误明确指出您所需的网址不存在路由。这意味着您必须考虑config/routes.rb文件,以及您如何定义上述路线。


<强>路线

根据您的错误判断,您应该建立一个路由文件:

#config/routes.rb
namespace :admin do
   resources :code_image #-> domain.com/admin/code_image/:id
end

需要注意的是,您需要确保code_image_controller.rb位于正确的位置。正确定义:

#app/controllers/admin/code_image_controller.rb #-> notice the /admin folder ;)
class CodeImageController < ApplicationController
   def show
      @code_image = CodeImage.find params[:id]
   end
end

<强> CRUD

您需要知道的是,当您在Rails中创建CRUD routes时,使用resources指令,您将获得以下自动创建的路径:

enter image description here

当您询问创建单独路线时,如果您使用我上面规定的路线,您应该没有加载特定资源的问题。


<强> ActiveAdmin

最后,只是为了解决任何困惑 - 这是一个调试练习。我们不使用ActiveAdmin,因此,我无法保证此解决方案是否可以使用它