我将此代码添加到users_controller.rb中的create操作中,并始终获取此错误。我用Google搜索并重置了浏览器设置,删除了Cookie等,但它无效。
我使用PG作为本地服务器。
任何人都可以看看这个并指引我走正确的道路
def create
@paper = current_user.papers.build(paper_params)
@electro = current_user.electros.build(electro_params)
@hwater = current_user.hwaters.build(hwater_params)
if @paper.save or @electro.save or @hwater.save
flash[:success] = "Messages sent."
redirect_to user_path(@user)
else
flash[:danger] = "Error occured, message has not been sent."
redirect_to user_path
end
end
之前,代码是这样的,它运行良好:
def create
@paper = current_user.papers.build(paper_params)
@electro = current_user.electros.build(electro_params)
if @paper.save, @electro.save, @hwater.save
flash[:success] = "Messages sent."
redirect_to paper_path(@paper)
else
flash[:danger] = "Error occured, message has not been sent."
redirect_to new_paper_path
end
@electro = current_user.electros.build(electro_params)
if @electro.save
flash[:success] = "Messages sent."
redirect_to electros_path(@electro)
else
flash[:danger] = "Error occured, message has not been sent."
redirect_to new_electros_path
end
end
这是users_controller.rb:
class UsersController < ApplicationController
before_action :set_paper, only: [:edit, :update, :show, :destroy]
before_action :set_electro, only: [:edit, :update, :show, :destroy]
before_action :set_hwater, only: [:edit, :update, :show, :destroy]
def index
@users = User.all
@users = User.order('created_at DESC').paginate(page: params[:page], per_page: 30)
end
def create
@paper = current_user.papers.build(paper_params)
@electro = current_user.electros.build(electro_params)
@hwater = current_user.hwaters.build(hwater_params)
if @paper.save or @electro.save or @hwater.save
flash[:success] = "Messages sent."
redirect_to user_path(@user)
else
flash[:danger] = "Error occured, message has not been sent."
redirect_to user_path
end
end
def show
@user = User.find(params[:id])
@users = User.order('created_at DESC').paginate(page: params[:page], per_page: 30)
@electro_total = current_user.electros.sum(:electricity_kwst)
@paper = current_user.papers.build
@electro = current_user.electros.build
@hwater = current_user.hwaters.build
end
def compare
if current_user.profile
@user = User.find_by(id: params[:to].to_i) if params[:to]
@paper_weight_total_user = @user.papers.sum(:paper_weight)
@paper_weight_per_capita_user = @user.papers.sum(:paper_weight) / (@user.profile.staff)
@env_paper_weight_user = @user.papers.sum(:env_paper_weight)
@env_paper_ratio_user = (@env_paper_weight_user / @paper_weight_total_user) * 100
@paper_tree_ratio_user = (@user.papers.sum(:paper_weight) / 1000) *15
@paper_tree_co2_rescue_user = @paper_tree_ratio_user * (0.492)
#Rafmagn
@electro_total_user = @user.electros.sum(:electricity_kwst)
@electro_total_per_capita_user = @user.electros.sum(:electricity_kwst) / @user.profile.staff
@electro_total_per_m2_user = @user.electros.sum(:electricity_kwst) / @user.profile.building_size
#HotWater
@hwater_total_m3_user = @user.hwaters.sum(:hot_water_cubic_meter)
@hwater_total_m3_per_capita_user = @user.hwaters.sum(:hot_water_cubic_meter) /@user.profile.staff
@hwater_m3_m2_ratio_user = @user.hwaters.sum(:hot_water_cubic_meter) / @user.profile.building_size
#Ræsting
@cleaning_total_user = @user.cleanings.sum(:cleaning_liter)
@cleaning_staff_ratio_user = @user.cleanings.sum(:cleaning_liter) / @user.profile.staff
@cleaning_building_ratio_user = @user.cleanings.sum(:cleaning_liter) / @user.profile.building_size
#Ferðalog-bilar
@transport_co2_km_user = @user.transports.sum(:transport_km) * (0.1404)
@transport_co2_km_staff_ratio_user = @user.transports.sum(:transport_km) * (0.1404) / @user.profile.staff
@transport_km_staff_ratio_user = @user.transports.sum(:transport_km) / @user.profile.staff
@transport_km_user = @user.transports.sum(:transport_km)
#Ferðalog-flug
@transport_flight_co2_user = @user.transports.sum(:transport_flight_km) * (0.1722)
@transport_flight_co2_staff_ratio_user = @user.transports.sum(:transport_flight_km) * (0.1722) / @user.profile.staff
@transport_flight_km_user = @user.transports.sum(:transport_flight_km)
@transport_flight_km_staff_ratio_user = @user.transports.sum(:transport_flight_km) / @user.profile.staff
#Co2 vegna ferðalaga
@co2_due_to_transport_user = (@transport_flight_co2 + @transport_co2_km) / 1000
@tree_count_rescue_user = @co2_due_to_transport * 492
else
redirect_to user_path
end
end
private
def set_paper
@paper = Paper.find(params[:id])
end
def paper_params
params.require(:paper).permit(:paper_type, :date, :paper_weight, :paper_cost, :env_paper_weight)
end
def set_electro
@electro = Electro.find(params[:id])
end
def electro_params
params.require(:electro).permit(:date, :building_name, :electricity_kwst, :electricity_cost, :kwst_staff_ratio, :kwst_square_meter_ratio, :user_id)
end
def set_hwater
@hwater = Hwater.find(params[:id])
end
def hwater_params
params.require(:hwater).permit(:date, :building_name, :hot_water_cost, :hot_water_cubic_meter, :hot_water_m2_m3_ratio, :hot_water_m3_staff_ratio, :user_id)
end
end
答案 0 :(得分:1)
我不完全确定您的应用程序的目标是什么,但这有点可疑:
if @paper.save or @electro.save or @hwater.save
flash[:success] = "Messages sent."
redirect_to user_path(@user)
else
flash[:danger] = "Error occured, message has not been sent."
redirect_to user_path
end
除了@BroiSatse在评论中所说的,第二个redirect_to
不应该redirect_to new_user_path
吗?像这样:
if @paper.save or @electro.save or @hwater.save
flash[:success] = "Messages sent."
redirect_to user_path(@user)
else
flash[:danger] = "Error occured, message has not been sent."
redirect_to new_user_path
end