Cookie signed报错:undefined method 'signed' 新建控制器路由后异常求助
Let's break down why this error popped up suddenly after adding a new controller and routes—even though your current_user method worked perfectly before.
1. Check the Parent Class of Your New Controller
The most common culprit here is that your new controller isn't inheriting from ApplicationController.
For example, if you created an API-focused controller, you might have accidentally written:
class Api::V1::NewController < ActionController::API # ... your actions end
Unlike ActionController::Base (which ApplicationController typically inherits from), ActionController::API doesn't include cookie-handling modules by default. That means the cookies object in this controller won't have the signed method.
Fixes:
- Either change the parent class to inherit from your
ApplicationController:class Api::V1::NewController < ApplicationController # ... - Or manually include the required cookie modules in the API controller:
class Api::V1::NewController < ActionController::API include ActionDispatch::Cookies include ActionDispatch::Session::CookieStore # Ensure your app's secret key is configured (check config/secrets.yml or credentials) end
2. Verify Your Route Configuration
Double-check your config/routes.rb to ensure the new route is pointing to the correct controller. It's easy to typo a controller name, leading the request to hit an unexpected controller that doesn't have access to cookies.signed.
For example, if you wrote:
get '/new-endpoint', to: 'wrong_controller#action'
And WrongController doesn't inherit from ApplicationController, you'll get this error when it tries to call current_user.
Fix:
Cross-reference the route's to: value with your actual controller class names (remember Rails uses snake_case for filenames and CamelCase for class names).
3. Confirm Cookie Middleware is Loaded
While you said you didn't update the app, it's worth checking if cookie-related middleware was accidentally removed. Run this command in your terminal to list all loaded middleware:
rails middleware
Look for ActionDispatch::Cookies and ActionDispatch::Session::CookieStore in the output. If they're missing, add them back to config/application.rb:
module YourAppName class Application < Rails::Application # ... config.middleware.use ActionDispatch::Cookies config.middleware.use ActionDispatch::Session::CookieStore end end
4. Pinpoint the Exact Request Causing the Error
Look at your server logs to find which request is triggering the error. You'll see a line like:
Processing by XxxController#yyy as HTML
This tells you exactly which controller is handling the problematic request. Once you have that, you can focus your debugging on that controller's inheritance and configuration.
内容的提问来源于stack exchange,提问作者Petr

