ruby-on-rails - 验证失败:作业无效(Ruby on Rails)
问题描述
我一直在关注 YouTube 上的这个系列,但现在我在尝试发布时遇到了这个错误。
https://www.youtube.com/watch?v=56GYV7Jj-uk&t=574s
我在 Rails 上遇到的这个问题有什么解决办法吗?
https://github.com/justalever/job_board/issues/
这是错误消息:
验证失败:作业无效创建 app/controllers/jobs_controller.rb,第 54 行
这是我的jobs_controller.rb:
class JobsController < ApplicationController
before_action :set_job, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
# GET /jobs
# GET /jobs.json
def index
@jobs = Job.all.order("created_at desc")
end
# GET /jobs/1
# GET /jobs/1.json
def show
end
# GET /jobs/new
def new
@job = current_user.jobs.build
end
# GET /jobs/1/edit
def edit
end
# POST /jobs
# POST /jobs.json
def create
@job = current_user.jobs.build(job_params)
token = params[:stripeToken]
job_type = params[:job_type]
job_title = params[:title]
card_brand = params[:user][:card_brand]
card_exp_month = params[:user][:card_exp_month]
card_exp_year = params[:user][:card_exp_year]
card_last4 = params[:user][:card_last4]
charge = Stripe::Charge.create(
:amount => 30000,
:currency => "usd",
:description => job_type,
:statement_descriptor => job_title,
:source => token
)
current_user.stripe_id = charge.id
current_user.card_brand = card_brand
current_user.card_exp_month = card_exp_month
current_user.card_exp_year = card_exp_year
current_user.card_last4 = card_last4
current_user.save!
respond_to do |format|
if @job.save
format.html { redirect_to @job, notice: 'Your job listing was purchased successfully!' }
format.json { render :show, status: :created, location: @job }
else
format.html { render :new }
format.json { render json: @job.errors, status: :unprocessable_entity }
end
end
rescue Stripe::CardError => e
flash.alert = e.message
render action: :new
end
# PATCH/PUT /jobs/1
# PATCH/PUT /jobs/1.json
def update
respond_to do |format|
if @job.update(job_params)
format.html { redirect_to @job, notice: 'Job was successfully updated.' }
format.json { render :show, status: :ok, location: @job }
else
format.html { render :edit }
format.json { render json: @job.errors, status: :unprocessable_entity }
end
end
end
# DELETE /jobs/1
# DELETE /jobs/1.json
def destroy
@job.destroy
respond_to do |format|
format.html { redirect_to jobs_url, notice: 'Job was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_job
@job = Job.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def job_params
params.require(:job).permit(:title, :description, :url, :job_type, :location, :job_author, :remote_ok, :apply_url, :avatar)
end
end
解决方案
current_user.save!
在您有机会尝试保存工作之前,您的调用会引发错误。尝试暂时更改current_user.save!
为current_user.save
,然后将错误信息添加到闪存中flash.alert = @job.errrors.full_messages.join(', ')
:)
# POST /jobs
# POST /jobs.json
def create
@job = current_user.jobs.build(job_params)
token = params[:stripeToken]
job_type = params[:job_type]
job_title = params[:title]
card_brand = params[:user][:card_brand]
card_exp_month = params[:user][:card_exp_month]
card_exp_year = params[:user][:card_exp_year]
card_last4 = params[:user][:card_last4]
charge = Stripe::Charge.create(
:amount => 30000,
:currency => "usd",
:description => job_type,
:statement_descriptor => job_title,
:source => token
)
current_user.stripe_id = charge.id
current_user.card_brand = card_brand
current_user.card_exp_month = card_exp_month
current_user.card_exp_year = card_exp_year
current_user.card_last4 = card_last4
current_user.save
respond_to do |format|
if @job.save
format.html { redirect_to @job, notice: 'Your job listing was purchased successfully!' }
format.json { render :show, status: :created, location: @job }
else
flash.alert = @job.errrors.full_messages.join(', ')
format.html { render :new }
format.json { render json: @job.errors, status: :unprocessable_entity }
end
end
end
推荐阅读
- angular - 如何让 Ngx 实现芯片双绑定
- devexpress - 如何在 DevExpress DateEdit 中显示波斯日历
- regex - 将字符串中的分数替换为小数
- java - 电子邮件服务的 Junit 测试
- java - 使用每个循环和映射创建 JSON 文件 - 它在 for 循环的所有迭代中重复第一个元素值
- android - 在 saveOnIstanceState() 之后 viewPager.setCurrentItem() 似乎不起作用
- java - Java 中的流比原生 for 循环慢得多 - 为什么?
- c# - 动态反序列化一个 json 文件是一些单元格是空的并且得到一个无效的数组
- xml - 如何在不将整个 XML 保存在内存中的情况下从 XML 文件中消除空叶元素?
- php - Symfony3 将表单数据传递给集合内的集合