Ruby on rails 未定义的方法`authenticate\u user!Api::Device/Rails 4中的PostsController
我的项目中有以下路径:Ruby on rails 未定义的方法`authenticate\u user!Api::Device/Rails 4中的PostsController,ruby-on-rails,devise,Ruby On Rails,Devise,我的项目中有以下路径: root 'home#index' namespace :api, defaults: { format: :json } do devise_for :users, controllers: { sessions: "api/sessions" } resources :posts end 用户模型: class User < ActiveRecord::Base has_many :posts, depend
root 'home#index'
namespace :api, defaults: { format: :json } do
devise_for :users, controllers: { sessions: "api/sessions" }
resources :posts
end
用户模型:
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
validates :name, presence: true
devise :database_authenticatable, :rememberable
end
class用户
会话控制器:
class Api::SessionsController < Devise::SessionsController
def create
@user = User.find_for_database_authentication(email: params[:user][:email])
if @user && @user.valid_password?(params[:user][:password])
sign_in(@user)
else
warden.custom_failure!
@errors = [ 'Invalid email or password' ]
render 'api/shared/errors', status: :unauthorized
end
end
end
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
#protect_from_forgery with: :exception
end
class Api::PostsController < ApplicationController
before_action :authenticate_user!, only: [ :create ]
def create
current_user.posts.create!(post_params)
end
private
def post_params
params.require(:post).permit(:title, :content)
end
end
class Api::SessionController
应用程序控制器:
class Api::SessionsController < Devise::SessionsController
def create
@user = User.find_for_database_authentication(email: params[:user][:email])
if @user && @user.valid_password?(params[:user][:password])
sign_in(@user)
else
warden.custom_failure!
@errors = [ 'Invalid email or password' ]
render 'api/shared/errors', status: :unauthorized
end
end
end
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
#protect_from_forgery with: :exception
end
class Api::PostsController < ApplicationController
before_action :authenticate_user!, only: [ :create ]
def create
current_user.posts.create!(post_params)
end
private
def post_params
params.require(:post).permit(:title, :content)
end
end
class ApplicationController
最后,我的邮政管理员:
class Api::SessionsController < Devise::SessionsController
def create
@user = User.find_for_database_authentication(email: params[:user][:email])
if @user && @user.valid_password?(params[:user][:password])
sign_in(@user)
else
warden.custom_failure!
@errors = [ 'Invalid email or password' ]
render 'api/shared/errors', status: :unauthorized
end
end
end
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
#protect_from_forgery with: :exception
end
class Api::PostsController < ApplicationController
before_action :authenticate_user!, only: [ :create ]
def create
current_user.posts.create!(post_params)
end
private
def post_params
params.require(:post).permit(:title, :content)
end
end
class Api::PostsController
但是当我尝试创建一个新的Post时,我得到了以下错误:“未定义的方法`authenticate\u user!Api::PostsController”。如果我删除它,我会得到关于“current_user”方法的错误。有什么问题吗?我怎样才能修好它?提前感谢 我在使用designe时遇到了类似的问题。 问题是我用不同的名称而不是用户(我叫xuser)命名了我的db。 在我的例子中,我所要做的就是重命名控制器中的变量。 像这样的
before_action :authenticate_xuser!, only: [ :create ]
def create
current_xuser.posts.create!(post_params)
end
private
def post_params
params.require(:post).permit(:title, :content)
end
希望有帮助 由于在
routes.rb
文件中的:api
命名空间中嵌套designe,因此会出现该错误。因此,您应该通过以下方式对用户进行身份验证:
class Api::PostsController < ApplicationController
before_action :authenticate_api_user!, only: [ :create ]
end
class Api::PostsController
这是一种非常奇怪的模式!