1

将新指南添加到我的应用程序时,显示操作出现错误。这是因为我更改了显示操作以允许自定义路线...新指南已正确添加到数据库中...

我在guidelines_controller.rb 中的显示动作是

def show
    @guideline = Guideline.where(title: params[:title]).first
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @guideline }

    end
  end

模型指南.rb 是

attr_accessible :content, :hospital, :title, :user_id, :guideline_id, :specialty

显示视图是

<p>Title: <%= link_to @guideline.title, seeguideline_path(@guideline.title) %> </p

错误信息是

控制台说……

Started POST "/guidelines" for 127.0.0.1 at 2013-02-22 17:07:29 +1100
Processing by GuidelinesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"bQKIkSb4Wzr46FERMbU82Q1qMzd3GrGNq6Nqmr0KNhY=", "guideline"=>{"title"=>"Stackoverflo", "specialty"=>"Dermatology", "hospital"=>"Stack Hospital", "content"=>"www.stackoverflow.com"}, "commit"=>"Create Guideline"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 11 LIMIT 1
   (0.1ms)  begin transaction
  Guideline Exists (0.4ms)  SELECT 1 AS one FROM "guidelines" WHERE (LOWER("guidelines"."hospital") = LOWER('Stack Hospital') AND "guidelines"."title" = 'Stackoverflo') LIMIT 1
  SQL (65.0ms)  INSERT INTO "guidelines" ("content", "created_at", "hospital", "specialty", "subtitle", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?)  [["content", "www.stackoverflow.com"], ["created_at", Fri, 22 Feb 2013 06:07:29 UTC +00:00], ["hospital", "Stack Hospital"], ["specialty", "Dermatology"], ["subtitle", nil], ["title", "Stackoverflo"], ["updated_at", Fri, 22 Feb 2013 06:07:29 UTC +00:00], ["user_id", 11]]
  SOLR Request (152.5ms)  [ path=#<RSolr::Client:0x007f9c79f2fc48> parameters={data: <?xml version="1.0" encoding="UTF-8"?><add><doc><field name="id">Guideline 35</field><field name="type">Guideline</field><field name="type">ActiveRecord::Base</field><field name="class_name">Guideline</field><field name="title_text">Stackoverflo</field></doc></add>, headers: {"Content-Type"=>"text/xml"}, method: post, params: {:wt=>:ruby}, query: wt=ruby, path: update, uri: http://localhost:8982/solr/update?wt=ruby, open_timeout: , read_timeout: } ]
   (3.7ms)  commit transaction
Redirected to http://localhost:3000/guidelines/35
  SOLR Request (100.3ms)  [ path=#<RSolr::Client:0x007f9c79f2fc48> parameters={data: <?xml version="1.0" encoding="UTF-8"?><commit/>, headers: {"Content-Type"=>"text/xml"}, method: post, params: {:wt=>:ruby}, query: wt=ruby, path: update, uri: http://localhost:8982/solr/update?wt=ruby, open_timeout: , read_timeout: } ]
Completed 302 Found in 485ms (ActiveRecord: 69.8ms)


Started GET "/guidelines/35" for 127.0.0.1 at 2013-02-22 17:07:30 +1100
Processing by GuidelinesController#show as HTML
  Parameters: {"id"=>"35"}
  Guideline Load (0.2ms)  SELECT "guidelines".* FROM "guidelines" WHERE "guidelines"."id" = ? LIMIT 1  [["id", "35"]]
  Guideline Load (0.2ms)  SELECT "guidelines".* FROM "guidelines" WHERE "guidelines"."title" IS NULL LIMIT 1
  Rendered guidelines/show.html.erb within layouts/application (18.3ms)
Completed 500 Internal Server Error in 83ms



ActionView::Template::Error (undefined method `title' for nil:NilClass):
    6:      
    7:          <div class="guideline span10">
    8:            
    9:          <p>Title: <%= link_to @guideline.title, seeguideline_path(@guideline.title) %> </p>
    10:             <strong> <a href="<%= @guideline.content %>", target = '_blank'>Link to guideline</a> </strong>
    11:             <p>Hospital Name: <%= @guideline.hospital %></p>
    12: 
  app/views/guidelines/show.html.erb:9:in `_app_views_guidelines_show_html_erb__4234346501713687788_70155056040280'
  app/controllers/guidelines_controller.rb:132:in `show'

路线是

get '/:title', to: 'guidelines#show', as: :seeguideline
4

1 回答 1

1

您确定您的路线正在通过 :title 参数吗?对于查询的 SQL 请求,您的控制台日志显示什么?我有一种感觉,您的路线并没有通过您认为正在通过的路线...

此外,您还需要处理查询返回为空的情况。让应用程序崩溃可能不是您想要的。

于 2013-02-22T04:48:55.537 回答