未定义的方法'标题'

时间:2013-02-22 03:41:36

标签: ruby-on-rails ruby-on-rails-3

当我的应用添加新指南时,我在show动作上遇到错误。这是因为我将show动作更改为允许自定义路径...新指南正确地添加到数据库中......

我在guidelines_controller.rb中的show动作是

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

show view is

<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

1 个答案:

答案 0 :(得分:1)

你确定你的路线正在通过:title param?您的控制台日志显示查询的SQL请求是什么?我有一种感觉,你的路线并没有超越你认为它正在经过的......

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