一个控制器使用其相应的布局,但另一个不使用

One controller uses its corresponding layout, but the other does not

我的 Rails 项目有两个控制器,一个 SessionsController 和一个 CustomersController。我的项目在布局目录中也有两个布局视图,sessions.html.erb 和 customers.html.erb.

我希望 SessionsController 使用 session.html.erb 布局呈现,而 CustomersController 使用 customers.html.erb 布局呈现。

我已经从我的项目中删除了所有明确的布局命令,据我所知,现在应该会出现所需的功能。

但是,实际发生的情况是 CustomersController 按预期使用 customers.html.erb 布局,但 SessionsController 根本没有使用任何布局。 (我完全删除了默认的 application.html.erb 布局,所以它没有什么可以依靠的。)

我还尝试在每个控制器中明确设置布局:“.html.erb”,但行为是一样的。

知道是什么原因造成的吗?

会话控制器:

class SessionsController < ApplicationController
  extend ActiveModel::Naming

  def initialize
    @errors = ActiveModel::Errors.new(self)
  end

  attr_accessor :name
  attr_reader   :errors

  def read_attribute_for_validation(attr)
    send(attr)
  end

  def SessionsController.human_attribute_name(attr, options = {} )
    attr
  end

  def SessionsController.lookup_ancestors
    [self]
  end

  def new
  end

  def create
    customer = Customer.find_by(customerID: params[:session][:user_id])
    if customer && customer.correct_password?(params[:session][:password])
      log_in customer
      remember customer
      customer.update_last_action_time
      redirect_to customer
    else
      @errors.add(:base, "UserID cannot be blank") if params[:session][:user_id].blank?
      @errors.add(:base, "Password cannot be blank") if params[:session][:password].blank?
      # Only push this final messag if there were no other errors
      @errors.add(:base, "Unrecognised UserID / password combination") if @errors.count == 0
      render "new"
    end
  end

  def destroy
    log_out if logged_in?
    redirect_to root_url
  end

end

客户控制器:

class CustomersController < ApplicationController

  before_action :logged_in_customer
  before_action :correct_customer

  def seo
#    @customer = Customer.find_by(params[:customerID])
  end

  def show
#    @customer = Customer.find_by(params[:customerID])
  end

  def edit
#   @customer = Customer.find_by(params[:customerID])
  end

  def update
#   @customer = Customer.find_by(params[:customerID])
    if @customer.update(customer_params)
      render "edit"
    else
      render "edit"
    end
  end

  private

  def customer_params
    params.require(:customer).permit( :new_email,
                                      :new_password,
                                      :new_password_confirmation,
                                      :existing_password,
                                      :update_type )
  end

  def logged_in_customer
    if logged_in? && !timed_out?
      # Intentionally blank
    else
      redirect_to login_url
    end
  end

  def correct_customer
    @customer = Customer.find(params[:id])
    redirect_to(customer_path(current_customer)) unless current_customer?(@customer)
  end

end

sessions.html.erb:

<%= render "shared/intro" %>

  <div class="header">
    <%= image_tag "rebrand.png", alt: "株式会社リブランド" %>
  </div>

  <div class="main-panel">
    <%= yield %>
  </div>

  <div class="footer">
    SEO-care
  </div>

<%= render "shared/outro" %>

customers.html.erb:

<%= render "shared/intro" %>

<%= render "shared/header" %>

<%= render "shared/left_panel" %>

<%= render "shared/top_panel" %>

  <!-- add any always-used formatting here -->
  <div class="main-panel">

    <%= yield %>

  <!-- add any always-used formatting here -->
  </div>
</div>

<%= render "shared/footer" %>

<%= render "shared/outro" %>

我没有包含部分内容,但我认为我可以排除它们,因为应用程序似乎甚至找不到布局文件,更不用说考虑其部分内容了。从每个控制器渲染页面时,可以在控制台输出中看到这一点:

会话数:

  Rendered sessions/new.html.erb (12.0ms)
Completed 200 OK in 30ms (Views: 20.0ms | ActiveRecord: 0.0ms)

客户:

  Rendered customers/show.html.erb within layouts/customers (0.0ms)
  Rendered shared/_intro.html.erb (183.1ms)
  Rendered shared/_header.html.erb (5.0ms)
  Rendered shared/_left_panel.html.erb (1.0ms)
  Rendered shared/_top_panel.html.erb (0.0ms)
  Rendered shared/_footer.html.erb (1.0ms)
  Rendered shared/_outro.html.erb (1.0ms)
Completed 200 OK in 249ms (Views: 203.2ms | ActiveRecord: 39.4ms)

快速解决方案:

initialize 方法似乎覆盖了基础 class 中的一个方法。一个快速的解决方案是确保使用 super:

也调用基本 initialize 方法
def initialize
  super
  @errors = ActiveModel::Errors.new(self)
end

可能更好的解决方案:

扩展 ActiveModel::Naming 以公开错误消息功能可能不是这种情况下处理错误的最佳方式;找到一种方法将错误添加到客户对象,然后使用这些错误(从而完全不需要扩展 ActiveModel::Naming)可能是首选的长期解决方案。感谢@maxcal 的建议。