Ruby on rails 保存不起作用,spree自定义方法
我正在开发我的SpreeRails应用程序,出于某些原因,我正在制作一些自定义方法。我在ProductsController中创建了一个新的和create方法,但最后一个方法不能正常工作,它不能将数据保存在我的数据库中,我不知道为什么 这是我的控制器:Ruby on rails 保存不起作用,spree自定义方法,ruby-on-rails,save,spree,Ruby On Rails,Save,Spree,我正在开发我的SpreeRails应用程序,出于某些原因,我正在制作一些自定义方法。我在ProductsController中创建了一个新的和create方法,但最后一个方法不能正常工作,它不能将数据保存在我的数据库中,我不知道为什么 这是我的控制器: module Spree class ProductsController < Spree::StoreController before_action :load_product, only: :show before
module Spree
class ProductsController < Spree::StoreController
before_action :load_product, only: :show
before_action :load_taxon, only: :index
rescue_from ActiveRecord::RecordNotFound, :with => :render_404
helper 'spree/taxons'
respond_to :html
def index
@searcher = build_searcher(params.merge(include_images: true))
@products = @searcher.retrieve_products
@taxonomies = Spree::Taxonomy.includes(root: :children)
end
#########################################by_cjdc#############################################################
def newproduct
@product = Product.new();
#render '/spree/home/newproduct'
end
def createproduct
@name = params[:product][:name];
@description = params[:product][:description];
@product = Product.new({
:id => 4,
:name => @name,
:description => @description});
@product.save();
if @product.save()
redirect_to "/tutienda", :notice => "El producto ha sido insertado";
else
render '/spree/products/newproduct'
end
end
######################################################################################################
def show
@variants = @product.variants_including_master.active(current_currency).includes([:option_values, :images])
@product_properties = @product.product_properties.includes(:property)
@taxon = Spree::Taxon.find(params[:taxon_id]) if params[:taxon_id]
end
private
def accurate_title
if @product
@product.meta_title.blank? ? @product.name : @product.meta_title
else
super
end
end
def load_product
if try_spree_current_user.try(:has_spree_role?, "admin")
@products = Product.with_deleted
else
@products = Product.active(current_currency)
end
@product = @products.friendly.find(params[:id])
end
def load_taxon
@taxon = Spree::Taxon.find(params[:taxon]) if params[:taxon].present?
end
end
end
模块狂欢
类ProductsController:render\u 404
助手“狂欢/分类”
回复:html
def索引
@searcher=build\u searcher(参数合并(包括图像:true))
@产品=@searcher.retrieve\u产品
@taxonomies=Spree::Taxonomy.includes(root::children)
终止
#########################################由联合总会主办#############################################################
def新产品
@product=product.new();
#渲染“/spree/home/newproduct”
终止
def createproduct
@name=params[:product][:name];
@description=params[:product][:description];
@product=product.new({
:id=>4,
:name=>@name,
:description=>@description});
@product.save();
如果@product.save()
将_重定向到“/tutienda”,注意=>“El producto ha sido insertado”;
其他的
呈现“/spree/products/newproduct”
终止
终止
######################################################################################################
def秀
@variants=@product.variants\u包括\u master.active(当前货币)。包括([:选项\u值,:图像])
@product_properties=@product.product_properties.includes(:property)
@taxon=Spree::taxon.find(params[:taxon\u id]),如果params[:taxon\u id]
终止
私有的
标题
如果@产品
@product.meta_title.blank@product.name:@product.meta_title
其他的
超级的
终止
终止
def load_产品
如果try\u spree\u当前用户。try(:has\u spree\u role?,“admin”)
@products=已删除的Product
其他的
@产品=产品。活动(当前货币)
终止
@product=@products.friendly.find(参数[:id])
终止
def装载分类单元
@taxon=Spree::taxon.find(params[:taxon]),如果params[:taxon]。是否存在?
终止
终止
终止
我的方法是newproduct和createproduct。我甚至在@product.save之前尝试了whitn和abort,以查看对象中的内容,对象中有数据,但DB没有得到数据。(对不起,我的英语不好)。你真正不需要的第一件事是在每行之后,ror不需要这样做 另一件事是,如果数据没有保存到数据库,那么您应该确保有任何验证失败。现在,如何检查哪些验证不允许保存。要知道用bang方法保存
save -> save!
!!如果任何验证失败,bang方法将抛出异常。
然后检查错误并修复它 将
保存
替换为保存代码>和修复验证问题。谢谢,效果很好,我是个傻瓜。