My Rails项目有两个控制器,一个是SessionsController和一个CustomersController。我的项目在layouts目录中有两个布局视图:sessions.html.erb和customers.html.erb。
我希望SessionsController使用session.html.erb布局和CustomersController进行渲染,以使用customers.html.erb布局进行渲染。
我已从项目中删除了所有显式布局命令,因此据我所知,现在应该发生所需的功能。
然而,实际发生的是CustomersController正在按预期使用customers.html.erb布局,但SessionsController根本没有使用布局。 (我完全删除了默认的application.html.erb布局,因此没有任何内容可以依赖。)
我还尝试使用布局明确设置每个控制器中的布局:“。html.erb”,但行为是相同的。
可能导致此问题的任何想法?
SessionsController:
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
CustomersController:
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)
答案 0 :(得分:1)
快速解决方案:
initialize
方法似乎覆盖了基类中的一个。快速解决方案是确保使用initialize
调用基本super
方法:
def initialize
super
@errors = ActiveModel::Errors.new(self)
end
可能更好的解决方案:
在这种情况下,扩展ActiveModel :: Naming以暴露错误消息功能可能不是处理错误的最佳方法;找到一种方法将错误添加到客户对象然后使用这些错误(从而无需扩展ActiveModel :: Naming)可能是一个首选的长期解决方案。感谢@maxcal提出此建议。