Rails 4: Change Database Values (修改数据库值) To change values in the database using Rails 4, you can use ActiveRecord's update methods. These methods allow you to update specific attributes of a record or multiple records at once. To update a single record,
Rails 4: Change Database Values (修改数据库值) To change values in the database using Rails 4, you can use ActiveRecord's update methods. These methods allow you to update specific attributes of a record or multiple records at once. To update a single record,
我对Rails还不太熟悉,在用户成功通过stripe支付后,我在更改数据库值方面遇到了困难。此外,在支付后,它每次都会以某种方式将我重定向到'/subscriberjobs/1',但该路径并不存在。相反,它应该直接跳转到应用程序的根路径。
以下是我的代码:
路由
resources :subscriberjobs resources :jobs
Jobs控制器
def new if current_user @job = current_user.jobs.build else redirect_to new_user_session_path end end def create @job = current_user.jobs.build(job_params) if @job.save redirect_to '/subscriberjobs/new' else render 'new' end end
Subscriberjobs控制器(以下内容无法正常工作!)
class SubscriberjobsController < ApplicationController before_filter :authenticate_user! def new end def update token = params[stripeToken] customer = Stripe::Customer.create( card: token, plan: 1004, email: current_user.email ) Job.is_active = true # 无法正常工作 Job.is_featured = false # 无法正常工作 Job.stripe_id = customer.id # 无法正常工作 Job.save # 无法正常工作 redirect_to root_path # 无法正常工作 end end
如果您需要更多信息,请告诉我。非常感谢您的回答!
Rails 4: 更改数据库值
在这个问题中,我们想要将保存的作业ID作为参数发送到subscriberjobs/new。我们可以在subscriberjobs/new的HTML表单中使用隐藏字段,将值job_id作为参数传递给SubscriberjobsController的update方法。然后我们可以通过params来访问它。
在JobController的create方法中,我们使用redirect_to
将作业ID作为参数传递给subscriberjobs/new页面:
redirect_to "/subscriberjobs/new?job_id=#{.id}"
在SubScribeJob的表单中,我们使用hidden_field_tag
来创建一个隐藏字段,将params[:job_id]的值赋给它:
hidden_field_tag 'job_id', params[:job_id]
在SubScribeJobController中,我们通过Job.find(params[:job_id])
来获取作业。
感谢您的帮助!我遇到了一个错误:undefined method 'merge' for nil:NilClass
,出现在表单行的= f.hidden_field 'job_id', params[:job_id]
。我从来没有遇到过这个错误。merge是什么意思?您可以在这个链接中查看更多信息:[rails hidden field undefined method merge error](http://stackoverflow.com/questions/6636875)
奇怪...我得到了正确的URL:http://localhost:3000/subscriberjobs/new?job_id=2
,但在更新后出现了这个错误:ActiveRecord::RecordNotFound in SubscriberjobsController#update Couldn't find Job with 'id'=
,然后它将我重定向到这个页面:
http://localhost:3000/subscriberjobs/1
,而is_active
的值仍然是false
。