Я использовал этот код в своих «пользовательских» представлениях, и у меня не было проблем: <% if current_user.admin?%>.Но использование его в наборе представлений, связанных с другим контроллером, выдает «Ошибка метода отсутствует».
Справочная информация: приложение позволяет администраторам создавать охотники за мусором.Администраторы должны иметь возможность удалять охоты.Я думал, что знаю, как все настроить, но, видимо, я что-то упустил.Вот мой код:
controller.rb
class HuntsController < ApplicationController
def index
@title = "All Hunts"
@hunts = Hunt.order("name ASC")
end
def show
@hunt = Hunt.find(params[:id])
@title = @hunt.name
end
def new
@hunt = Hunt.new
@title = "New Hunt"
end
def create
@hunt = Hunt.new(params[:hunt])
if @hunt.save
flash[:success] = "Hunt created!"
redirect_to hunts
else
@title = "New Hunt"
render 'new'
end
end
def edit
@hunt = Hunt.find(params[:id])
@title = "Edit hunt"
end
def delete
Hunt.find(params[:id]).destroy
flash[:success] = "Hunt destroyed."
redirect_to index
end
end
Views / Index.html.erb
<h1>All Hunts</h1>
<ul>
<% @hunts.each do |hunt| %>
<%= render hunt %>
<% end %>
</ul>
<%= link_to( "Create New Hunt", '/hunts/new') %>
Views / _hunt.html.erb
<li>
<%= link_to hunt.name, hunt %>
<% if current_user.admin? %>
<%= link_to "delete", hunt, :method => :delete, :confirm => "You sure?",
:title => "Delete #{hunt.name}" %>
<% end %>
</li>
Сообщение об ошибке при попытке перейти на / hunts:
NoMethodError in Hunts#index
Showing ...../app/views/hunts/_hunt.html.erb where line #3 raised:
undefined method `admin?' for nil:NilClass