我有一个 before_filter
通常会验证每个 Controller ;但是我不将它用于某个 Controller 。但是,当我从该 Controller 访问特定操作时,由于某种原因,我收到一条错误消息,指出用于身份验证的 Rails - 参数数量错误 (:force => true?)
。我在方法中添加了一个参数,以查看传递给该方法的内容,并将其视为参数 {:force=>true}
。任何其他 Controller 操作都不会发生这种情况。
这是我的错误
ArgumentError in RegistrationsController#edit
wrong number of arguments (1 for 0)
Rails.root: *
Application Trace | Framework Trace | Full Trace
app/controllers/application_controller.rb:6:in `authenticate_user!'
这是我尝试访问的路线
edit_user_registration GET /users/edit(.:format) registrations#edit
这是我的 Controller
class RegistrationsController < Devise::RegistrationsController
def build_resource(*args)
super
if session[:omniauth]
@user.apply_omniauth(session[:omniauth])
@user.valid?
end
end
def edit_password
end
end
这是 before_filter 方法
class ApplicationController < ActionController::Base
protect_from_forgery
private
def authenticate_user!
unless signed_in?
redirect_to '/users/sign_in'
end
end
end
顺便说一句,路线是由 devise 提供给我的
最佳答案
过滤器是由devise
提供的,所以不要试图弯曲它。
在所需的 Controller
中使用它。
before_filter :authenticate_user!
如果您正在寻找处理未经身份验证的用户,您可以在 routes
文件中执行此操作
authenticated :user do
root :to => "users#index"
end
unauthenticated :user do
devise_scope :user do
get "/" => "sessions#new"
end
end
希望下面的部分已经设置
devise_for :users, :controllers => {:registrations => "registrations", :sessions => "sessions"}
关于ruby-on-rails - Rails - 参数数量错误(:force => true?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24255219/