【发布时间】:2016-10-12 20:33:39
【问题描述】:
我在 rails 和 ajax 中有一个基本的 CRUD,但是我需要集成 Devise gem,当我将 devise gem 添加到我的项目并测试 CRUD 时,我收到了这个错误:
rails 4.2 中的控制器代码:
class ProductsController < ApplicationController
before_action :authenticate_user!
before_action :product_find, only: [:show, :update, :destroy, :edit]
def index
@products = Product.all.order('created_at DESC')
end
def show
end
def new
@product = Product.new
respond_to do |format|
format.html { render layout: false }
format.json { render json: @product }
format.js
end
end
def create
@product = current_user.products.build(product_params)
respond_to do |format|
if @product.save
format.json { render :show, status: :created, location: @product}
format.js
else
format.html { render :new }
format.json { render json: @product.errors, status: :unprocessable_entity }
format.js
end
end
end
def edit
end
def update
@product = Product.update(params[:id], product_params)
end
def destroy
@product.destroy
end
private
def product_find
@product = Product.where(id: params[:id]).first
end
def product_params
params.require(:product).permit(:name, :quantity, :price)
end
end
用户可以正常连接,当我用注册用户创建新产品时出现错误
我做错了什么?
我认为错误可能在 CREATE 和 NEW 方法中
谢谢!
【问题讨论】:
-
您确定您的用户在收到错误时已登录?
-
用户可以正常连接,注册用户创建新产品时出现错误
-
当你得到错误时是
current_usernon-null? -
我认为错误可能在 CREATE 和 NEW 方法中
标签: ruby-on-rails ajax devise