Я следую этому учебнику (раздел аутентификации), и я столкнулся с этой ошибкой при попытке доступа к localhost: 3000 / авторы / новые
views/authors/_form.html.erb where line #2 raised:
undefined method `errors' for nil:NilClass
Extracted source (around line #2):
<%= form_with(model: author, local: true) do |form| %>
<% if author.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(author.errors.count, "error") %> prohibited this author from being saved:</h2>
<ul>
Ошибка появилась при добавлении метод before_filter и zero_authors_or_authenticated в моем файлеhors_controller.
class AuthorsController < ApplicationController
before_action :set_author, only: [:show, :edit, :update, :destroy]
before_filter :zero_authors_or_authenticated, only: [:new, :create]
def zero_authors_or_authenticated
unless Author.count == 0 || current_user
redirect_to root_path
return false
end
end
# GET /authors
# GET /authors.json
def index
@authors = Author.all
end
# GET /authors/1
# GET /authors/1.json
def show
end
# GET /authors/new
def new
@author = Author.new
end
# GET /authors/1/edit
def edit
end
# POST /authors
# POST /authors.json
def create
@author = Author.new(author_params)
respond_to do |format|
if @author.save
format.html { redirect_to @author, notice: 'Author was successfully created.' }
format.json { render :show, status: :created, location: @author }
else
format.html { render :new }
format.json { render json: @author.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /authors/1
# PATCH/PUT /authors/1.json
def update
respond_to do |format|
if @author.update(author_params)
format.html { redirect_to @author, notice: 'Author was successfully updated.' }
format.json { render :show, status: :ok, location: @author }
else
format.html { render :edit }
format.json { render json: @author.errors, status: :unprocessable_entity }
end
end
end
# DELETE /authors/1
# DELETE /authors/1.json
def destroy
@author.destroy
respond_to do |format|
format.html { redirect_to authors_url, notice: 'Author was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_author
@author = Author.find(params[:id])
end
# Only allow a list of trusted parameters through.
def author_params
params.require(:author).permit(:username, :email, :password, :password_confirmation)
end
end
Вот целые представления / авторы / _form. html .erb, если необходимо
<%= form_with(model: author, local: true) do |form| %>
<% if author.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(author.errors.count, "error") %> prohibited this author from being saved:</h2>
<ul>
<% author.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :username %>
<%= form.text_field :username %>
</div>
<div class="field">
<%= form.label :email %>
<%= form.text_field :email %>
</div>
<div class="field">
<%= form.label :password %>
<%= form.text_field :password %>
</div>
<div class="field">
<%= form.label :password_confirmation %>
<%= form.text_field :password_confirmation %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
Я попытался переместить часть before_filter дальше вниз, но затем он просто ничего не делает и Я искал любые орфографические ошибки, насколько я могу судить.
авторы / нов. html .erb
<h1>New Author</h1>
<%= render 'form', author: @author %>
<%= link_to 'Back', authors_path %>
Полный след от сообщения об ошибке
actionview (6.0.2.1) lib/action_view/helpers/capture_helper.rb:45:in `block in capture'
actionview (6.0.2.1) lib/action_view/helpers/capture_helper.rb:209:in `with_output_buffer'
actionview (6.0.2.1) lib/action_view/helpers/capture_helper.rb:45:in `capture'
actionview (6.0.2.1) lib/action_view/helpers/form_helper.rb:755:in `form_with'
app/views/authors/_form.html.erb:1
actionview (6.0.2.1) lib/action_view/base.rb:274:in `_run'
actionview (6.0.2.1) lib/action_view/template.rb:185:in `block in render'
activesupport (6.0.2.1) lib/active_support/notifications.rb:182:in `instrument'
actionview (6.0.2.1) lib/action_view/template.rb:386:in `instrument_render_template'
actionview (6.0.2.1) lib/action_view/template.rb:183:in `render'
actionview (6.0.2.1) lib/action_view/renderer/partial_renderer.rb:358:in `block in render_partial'
actionview (6.0.2.1) lib/action_view/renderer/abstract_renderer.rb:89:in `block in instrument'
activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in `block in instrument'
activesupport (6.0.2.1) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in `instrument'
actionview (6.0.2.1) lib/action_view/renderer/abstract_renderer.rb:88:in `instrument'
actionview (6.0.2.1) lib/action_view/renderer/partial_renderer.rb:347:in `render_partial'
actionview (6.0.2.1) lib/action_view/renderer/partial_renderer.rb:317:in `render'
actionview (6.0.2.1) lib/action_view/renderer/renderer.rb:65:in `render_partial_to_object'
actionview (6.0.2.1) lib/action_view/renderer/renderer.rb:53:in `render_partial'
actionview (6.0.2.1) lib/action_view/helpers/rendering_helper.rb:38:in `render'
app/views/authors/new.html.erb:3
actionview (6.0.2.1) lib/action_view/base.rb:274:in `_run'
actionview (6.0.2.1) lib/action_view/template.rb:185:in `block in render'
activesupport (6.0.2.1) lib/active_support/notifications.rb:182:in `instrument'
actionview (6.0.2.1) lib/action_view/template.rb:386:in `instrument_render_template'
actionview (6.0.2.1) lib/action_view/template.rb:183:in `render'
actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:59:in `block (2 levels) in render_template'
actionview (6.0.2.1) lib/action_view/renderer/abstract_renderer.rb:89:in `block in instrument'
activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in `block in instrument'
activesupport (6.0.2.1) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in `instrument'
actionview (6.0.2.1) lib/action_view/renderer/abstract_renderer.rb:88:in `instrument'
actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:58:in `block in render_template'
actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:66:in `render_with_layout'
actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:57:in `render_template'
actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:13:in `render'
actionview (6.0.2.1) lib/action_view/renderer/renderer.rb:61:in `render_template_to_object'
actionview (6.0.2.1) lib/action_view/renderer/renderer.rb:29:in `render_to_object'
actionview (6.0.2.1) lib/action_view/rendering.rb:118:in `block in _render_template'
actionview (6.0.2.1) lib/action_view/base.rb:304:in `in_rendering_context'
actionview (6.0.2.1) lib/action_view/rendering.rb:117:in `_render_template'
actionpack (6.0.2.1) lib/action_controller/metal/streaming.rb:219:in `_render_template'
actionview (6.0.2.1) lib/action_view/rendering.rb:103:in `render_to_body'
actionpack (6.0.2.1) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (6.0.2.1) lib/action_controller/metal/renderers.rb:142:in `render_to_body'
actionpack (6.0.2.1) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (6.0.2.1) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (6.0.2.1) lib/active_support/core_ext/benchmark.rb:14:in `block in ms'
/home/xxx/.rbenv/versions/2.6.4/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
activesupport (6.0.2.1) lib/active_support/core_ext/benchmark.rb:14:in `ms'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:85:in `cleanup_view_runtime'
activerecord (6.0.2.1) lib/active_record/railties/controller_runtime.rb:34:in `cleanup_view_runtime'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:43:in `render'
actionpack (6.0.2.1) lib/action_controller/metal/implicit_render.rb:35:in `default_render'
actionpack (6.0.2.1) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (6.0.2.1) lib/abstract_controller/base.rb:196:in `process_action'
actionpack (6.0.2.1) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (6.0.2.1) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:135:in `run_callbacks'
actionpack (6.0.2.1) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (6.0.2.1) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:33:in `block in process_action'
activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in `block in instrument'
activesupport (6.0.2.1) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in `instrument'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:32:in `process_action'
actionpack (6.0.2.1) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (6.0.2.1) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
actionpack (6.0.2.1) lib/abstract_controller/base.rb:136:in `process'
actionview (6.0.2.1) lib/action_view/rendering.rb:39:in `process'
actionpack (6.0.2.1) lib/action_controller/metal.rb:191:in `dispatch'
actionpack (6.0.2.1) lib/action_controller/metal.rb:252:in `dispatch'
actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:51:in `dispatch'
actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:33:in `serve'
actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:49:in `block in serve'
actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:32:in `each'
actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:837:in `call'
rack (2.2.2) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.2.2) lib/rack/etag.rb:27:in `call'
rack (2.2.2) lib/rack/conditional_get.rb:27:in `call'
rack (2.2.2) lib/rack/head.rb:12:in `call'
actionpack (6.0.2.1) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.2.2) lib/rack/session/abstract/id.rb:266:in `context'
rack (2.2.2) lib/rack/session/abstract/id.rb:260:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/cookies.rb:648:in `call'
activerecord (6.0.2.1) lib/active_record/migration.rb:567:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:101:in `run_callbacks'
actionpack (6.0.2.1) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:32:in `call'
web-console (4.0.1) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.0.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (4.0.1) lib/web_console/middleware.rb:17:in `catch'
web-console (4.0.1) lib/web_console/middleware.rb:17:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.0.2.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (6.0.2.1) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:in `block in tagged'
activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:in `tagged'
railties (6.0.2.1) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.2.2) lib/rack/method_override.rb:24:in `call'
rack (2.2.2) lib/rack/runtime.rb:22:in `call'
activesupport (6.0.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/static.rb:126:in `call'
rack (2.2.2) lib/rack/sendfile.rb:110:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/host_authorization.rb:83:in `call'
webpacker (4.2.2) lib/webpacker/dev_server_proxy.rb:23:in `perform_request'
rack-proxy (0.6.5) lib/rack/proxy.rb:57:in `call'
railties (6.0.2.1) lib/rails/engine.rb:526:in `call'
puma (4.3.1) lib/puma/configuration.rb:228:in `call'
puma (4.3.1) lib/puma/server.rb:681:in `handle_request'
puma (4.3.1) lib/puma/server.rb:472:in `process_client'
puma (4.3.1) lib/puma/server.rb:328:in `block in run'
puma (4.3.1) lib/puma/thread_pool.rb:134:in `block in spawn_thread'