我刚刚在Heroku上为我的应用创建了一个管理员资料。我按照here描述的方式使用pgAdminIII进入我的Heroku生产数据库,在数据库中我只是将用户配置文件的管理字段更改为true
。
到目前为止,我在大多数应用程序中都有管理员访问权限但是当我尝试在应用程序上查看我的管理员个人资料时出现错误我们很抱歉,但出了点问题。当我使用普通用户配置文件登录时,我仍然可以轻松查看该配置文件。
heroku日志说有一个undefined method 'name'
,但我不明白这个错误,因为当我尝试访问生产环境中的其他配置文件或我的开发环境中的任何配置文件时,它都没有显示。
我的if current_user.try(:admin?)
中确实存在users/show.html.erb
条件,但这并没有在我的开发环境中丢失任何错误。
我不确定这里出了什么问题,我是网络开发的新手,通过pgAdmin编辑生产数据库是不是很糟糕?它似乎已经在工作中投入了扳手。
Heroku登录管理员资料
2013-04-19T14:06:10.911478+00:00 app[web.1]: => Booting WEBrick
2013-04-19T14:06:10.911478+00:00 app[web.1]: => Rails 3.2.12 application starting in production on http://0.0.0.0:54611
2013-04-19T14:06:10.911478+00:00 app[web.1]: => Call with -d to detach
2013-04-19T14:06:10.911478+00:00 app[web.1]: => Ctrl-C to shutdown server
2013-04-19T14:06:10.911478+00:00 app[web.1]: Started GET "/users/1" for 86.47.2.244 at 2013-04-19 14:06:10 +0000
2013-04-19T14:06:11.037505+00:00 app[web.1]: Processing by UsersController#show as HTML
2013-04-19T14:06:11.037505+00:00 app[web.1]: Parameters: {"id"=>"1"}
2013-04-19T14:06:11.439430+00:00 app[web.1]: Completed 500 Internal Server Error in 402ms
2013-04-19T14:06:11.441222+00:00 app[web.1]: ActionView::Template::Error (undefined method `name' for nil:NilClass):
2013-04-19T14:06:11.441222+00:00 app[web.1]: 28: <tr>
2013-04-19T14:06:11.441222+00:00 app[web.1]: 29: <% @user.photos.each do |photo| %>
2013-04-19T14:06:11.441222+00:00 app[web.1]: 30: <td class='user_pics'>
2013-04-19T14:06:11.439430+00:00 app[web.1]: Rendered users/show.html.erb within layouts/application (283.4ms)
2013-04-19T14:06:11.441222+00:00 app[web.1]: 33: <%= link_to t('.destroy', :default => t("helpers.links.destroy")), photo_path(photo),:method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },:class => 'btn btn-mini btn-danger' %>
2013-04-19T14:06:11.441222+00:00 app[web.1]: 34: </td>
2013-04-19T14:06:11.441222+00:00 app[web.1]:
2013-04-19T14:06:11.441222+00:00 app[web.1]: 31: <h6><%= get_hike_name_by_photo(photo.hikingtrail_id) %></h6>
2013-04-19T14:06:11.441495+00:00 app[web.1]: app/views/users/show.html.erb:31:in `block in _app_views_users_show_html_erb__1466921769571898123_36255720'
2013-04-19T14:06:11.441222+00:00 app[web.1]: app/helpers/users_helper.rb:4:in `get_hike_name_by_photo'
2013-04-19T14:06:11.441222+00:00 app[web.1]: 32: <%= image_tag photo.image_url(:thumb).to_s %> <br />
2013-04-19T14:06:11.441495+00:00 app[web.1]:
2013-04-19T14:06:11.441495+00:00 app[web.1]: app/views/users/show.html.erb:29:in `_app_views_users_show_html_erb__1466921769571898123_36255720'
2013-04-19T14:06:11.441495+00:00 app[web.1]:
2013-04-19T14:06:11.445147+00:00 heroku[router]: at=info method=GET path=/users/1 host=hiking-project.herokuapp.com fwd="86.47.2.244" dyno=web.1 connect=1ms service=548ms status=500 bytes=643
Heroku记录非管理员资料GET
2013-04-19T14:07:42.861730+00:00 app[web.1]: Parameters: {"id"=>"2"}
2013-04-19T14:07:42.857372+00:00 app[web.1]: Started GET "/users/2" for 86.47.2.244 at 2013-04-19 14:07:42 +0000
2013-04-19T14:07:42.861675+00:00 app[web.1]: Processing by UsersController#show as HTML
2013-04-19T14:07:42.889420+00:00 app[web.1]: Rendered users/show.html.erb within layouts/application (24.2ms)
2013-04-19T14:07:42.900768+00:00 app[web.1]: Rendered layouts/_navbar.html.erb (2.2ms)
2013-04-19T14:07:42.901987+00:00 app[web.1]: Completed 200 OK in 40ms (Views: 28.3ms | ActiveRecord: 11.0ms)
2013-04-19T14:07:42.901785+00:00 app[web.1]: Rendered layouts/_footer.html.erb (0.6ms)
2013-04-19T14:07:42.905317+00:00 heroku[router]: at=info method=GET path=/users/2 host=hiking-project.herokuapp.com fwd="86.47.2.244" dyno=web.1 connect=1ms service=53ms status=200 bytes=6621
2013-04-19T14:07:43.100688+00:00 heroku[router]: at=info method=GET path=/assets/application-5343b16e766707937994ff5eeed67d4a.css host=hiking-project.herokuapp.com fwd="86.47.2.244" dyno=web.1 connect=2ms service=6ms status=304 bytes=0
2013-04-19T14:07:43.209119+00:00 heroku[router]: at=info method=GET path=/assets/application-2f6741be57170d2a4f223dd28b4827c8.js host=hiking-project.herokuapp.com fwd="86.47.2.244" dyno=web.1 connect=0ms service=4ms status=304 bytes=0
2013-04-19T14:07:43.263057+00:00 heroku[router]: at=info method=GET path=/uploads/photo/image/12/thumb_View_from_spink.jpg host=hiking-project.herokuapp.com fwd="86.47.2.244" dyno=web.1 connect=0ms service=25ms status=404 bytes=728
2013-04-19T14:07:43.256301+00:00 app[web.1]: ActionController::RoutingError (No route matches [GET] "/uploads/photo/image/11/thumb_spink_sleepers.jpg"):
2013-04-19T14:07:43.256301+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/rack/logger.rb:32:in `call_app'
2013-04-19T14:07:43.256301+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
2013-04-19T14:07:43.256301+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/rack/logger.rb:16:in `block in call'
2013-04-19T14:07:43.256566+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/engine.rb:479:in `call'
2013-04-19T14:07:43.261546+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/lock.rb:15:in `call'
2013-04-19T14:07:43.261546+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/middleware/static.rb:62:in `call'
2013-04-19T14:07:43.256817+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/content_length.rb:14:in `call'
2013-04-19T14:07:43.256817+00:00 app[web.1]: /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
2013-04-19T14:07:43.256817+00:00 app[web.1]: /usr/local/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
2013-04-19T14:07:43.261546+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:136:in `forward'
2013-04-19T14:07:43.261546+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:245:in `fetch'
2013-04-19T14:07:43.261546+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:185:in `lookup'
2013-04-19T14:07:43.261546+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
2013-04-19T14:07:43.256566+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/lock.rb:15:in `call'
2013-04-19T14:07:43.256566+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:136:in `forward'
2013-04-19T14:07:43.256566+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:66:in `call!'
2013-04-19T14:07:43.261546+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:66:in `call!'
2013-04-19T14:07:43.261546+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:51:in `call'
2013-04-19T14:07:43.261546+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/engine.rb:479:in `call'
2013-04-19T14:07:43.261295+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/tagged_logging.rb:22:in `tagged'
2013-04-19T14:07:43.256301+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
2013-04-19T14:07:43.261295+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/rack/logger.rb:32:in `call_app'
2013-04-19T14:07:43.256301+00:00 app[web.1]:
2013-04-19T14:07:43.261755+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/application.rb:223:in `call'
2013-04-19T14:07:43.261755+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/content_length.rb:14:in `call'
2013-04-19T14:07:43.261755+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/rack/log_tailer.rb:17:in `call'
2013-04-19T14:07:43.261295+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/rack/logger.rb:16:in `call'
2013-04-19T14:07:43.256301+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/tagged_logging.rb:22:in `tagged'
2013-04-19T14:07:43.256301+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/rack/logger.rb:16:in `call'
2013-04-19T14:07:43.256817+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/handler/webrick.rb:59:in `service'
2013-04-19T14:07:43.261755+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/handler/webrick.rb:59:in `service'
2013-04-19T14:07:43.261
755+00:00 app[web.1]: /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
2013-04-19T14:07:43.261295+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/rack/logger.rb:16:in `block in call'
2013-04-19T14:07:43.256301+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/methodoverride.rb:21:in `call'
2013-04-19T14:07:43.256817+00:00 app[web.1]:
2013-04-19T14:07:43.261755+00:00 app[web.1]: /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
2013-04-19T14:07:43.261755+00:00 app[web.1]: /usr/local/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
2013-04-19T14:07:43.256566+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/runtime.rb:17:in `call'
2013-04-19T14:07:43.248790+00:00 app[web.1]: Started GET "/uploads/photo/image/11/thumb_spink_sleepers.jpg" for 86.47.2.244 at 2013-04-19 14:07:43 +0000
2013-04-19T14:07:43.256817+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/rack/log_tailer.rb:17:in `call'
2013-04-19T14:07:43.261295+00:00 app[web.1]:
2013-04-19T14:07:43.261295+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
2013-04-19T14:07:43.256817+00:00 app[web.1]:
2013-04-19T14:07:43.258182+00:00 app[web.1]: Started GET "/uploads/photo/image/12/thumb_View_from_spink.jpg" for 86.47.2.244 at 2013-04-19 14:07:43 +0000
2013-04-19T14:07:43.261295+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/middleware/request_id.rb:22:in `call'
2013-04-19T14:07:43.261295+00:00 app[web.1]: ActionController::RoutingError (No route matches [GET] "/uploads/photo/image/12/thumb_View_from_spink.jpg"):
2013-04-19T14:07:43.261295+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
2013-04-19T14:07:43.261295+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/methodoverride.rb:21:in `call'
2013-04-19T14:07:43.256301+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/middleware/request_id.rb:22:in `call'
2013-04-19T14:07:43.256566+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/middleware/static.rb:62:in `call'
2013-04-19T14:07:43.261755+00:00 app[web.1]:
2013-04-19T14:07:43.261755+00:00 app[web.1]:
2013-04-19T14:07:43.256566+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:245:in `fetch'
2013-04-19T14:07:43.256817+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/application.rb:223:in `call'
2013-04-19T14:07:43.256817+00:00 app[web.1]: /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
2013-04-19T14:07:43.261546+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/runtime.rb:17:in `call'
2013-04-19T14:07:43.256566+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
2013-04-19T14:07:43.256566+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:185:in `lookup'
2013-04-19T14:07:43.256566+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:51:in `call'
2013-04-19T14:07:43.257395+00:00 heroku[router]: at=info method=GET path=/uploads/photo/image/11/thumb_spink_sleepers.jpg host=hiking-project.herokuapp.com fwd="86.47.2.244" dyno=web.1 connect=0ms service=12ms status=404 bytes=728
用户/ show.html.erb
<div class="page-header">
<h1 class='float_left'>Your Profile</h1>
<%= link_to "Edit your Profile", edit_user_path(current_user), :class => 'btn btn-primary float_right' %>
<div class='clear_float'></div>
</div>
<p>
<b>username:</b>
<%= @user.user_name %>
</p>
<p>
<b>email:</b>
<%= @user.email %>
</p>
<div class="accordion" id="accordion2">
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
Your Photos
</a>
</div>
<div id="collapseOne" class="accordion-body collapse in">
<div class="accordion-inner">
<table>
<tr>
<% @user.photos.each do |photo| %>
<td class='user_pics'>
<h6><%= get_hike_name_by_photo(photo.hikingtrail_id) %></h6>
<%= image_tag photo.image_url(:thumb).to_s %> <br />
<%= link_to t('.destroy', :default => t("helpers.links.destroy")), photo_path(photo),:method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },:class => 'btn btn-mini btn-danger' %>
</td>
<% end %>
</tr>
</table>
</div>
</div>
</div>
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo">
Hikes You Created
</a>
</div>
<div id="collapseTwo" class="accordion-body collapse">
<div class="accordion-inner">
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Brief Description</th>
<th style="min-width:120px;">Location</th>
<th>Country</th>
<th>Looped</th>
<th style="min-width:100px;">Duration</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @user.hikingtrails.each do |hikingtrail| %><tr>
<tr>
<td><%= link_to hikingtrail.name, hikingtrail_path(hikingtrail) %></td>
<td><%= truncate(hikingtrail.description, :length => 95, :separator => ' ') %></td>
<!-- <td><%= hikingtrail.description %></td> -->
<td><%= hikingtrail.addr_2.capitalize %>, <%= hikingtrail.addr_3.capitalize %></td>
<td><%= hikingtrail.country %></td>
<td><%= boolean_to_yes_or_no(hikingtrail.looped) %></td>
<td><%= hikingtrail.duration_hours %>hrs <%= hikingtrail.duration_mins %>mins</td>
<td>
<%= link_to t('.edit', :default => t("helpers.links.edit")),edit_hikingtrail_path(hikingtrail), :class => 'btn btn-mini' %>
<% if current_user.try(:admin?) %>
<%= link_to t('.destroy', :default => t("helpers.links.destroy")),
hikingtrail_path(hikingtrail),
:method => :delete,
:data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
:class => 'btn btn-mini btn-danger' %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
答案 0 :(得分:1)
每个hikingtrail
都有name
个值吗?当您收到NilClass
错误时,这意味着您正在尝试拨打不存在的内容。在这种情况下,undefined method 'name' for nil:NilClass
表示您要求的name
不在数据库中。检查这一行:
<td><%= link_to hikingtrail.name, hikingtrail_path(hikingtrail) %></td>
并将其更改为:
<td><%= link_to hikingtrail.name, hikingtrail_path(hikingtrail) if hikingtrail.name.present? %></td>