我想检查当前用户是否属于公司,是否重定向到root_path
或重定向到new_company_path
。
我得到了
"NoMethodError in ExpensesController#new undefined method `company_user'
for #<ExpensesController:0x57ba1c8>"
但我尝试了不同的方式,但仍然没有工作,如果你能告诉我问题在哪里,那将是非常友好的。感谢。
应用/控制器/ application_controller.rb
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protect_from_forgery with: :exception
before_action :authenticate_user!
before_filter *(I also tried before_action)* :company_user
private
layout :layout_by_resource
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u|
u.permit(:username,
:country,
:email,
:password)
}
end
def layout_by_resource
if devise_controller?
"login"
else
"application"
en
end
结束&lt;从company_user移动
def company_user
if current_user.companies.nil?
redirect_to new_companies_path
else
redirect_to root_path
end
end
end <the issue was there, I moved it to layout_by_resource.
end
编辑 ** User.rb **
class User < ActiveRecord::Base**
#default_value_for :role, "1"
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :timesheets
has_many :expenses
has_many :jobdescriptions
belongs_to :companies
end
&#13;
费用控制器
class ExpensesController < ApplicationController
layout 'application'
def new
@expense = Expense.new
end
def create
@expense = current_user.expenses.create(expense_params)
redirect_to new_expense_path
end
# def serve
# @expense = Expense.find(params[:id])
# send_data(@expense.data, :type => @expense.mime_type, :filename => "#{@expense.name}.jpg", :disposition => "inline")
# end
private
def expense_params
params.require(:expense).permit(:amount, :user_id, :weeknum, :data, :date,)
end
end
&#13;