【问题标题】:Rails 4: Change Database ValuesRails 4:更改数据库值
【发布时间】:2016-09-11 10:53:26
【问题描述】:

我对 Rails 还很陌生,在用户通过条带成功付款后,我一直在努力更改数据库值。此外,付款后,它每次都会以某种方式将我重定向到不存在的“/subscriberjobs/1”。相反,它应该指向应用程序的 root_path。

这是我得到的:

路线

resources :subscriberjobs
resources :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 # doesn't work
        Job.is_featured = false # doesn't work
        Job.stripe_id = customer.id # doesn't work
        Job.save # doesn't work

        redirect_to root_path # doesn't work

    end
end

如果您需要更多信息,请告诉我。每个答案都非常感谢。谢谢!

【问题讨论】:

  • 有什么错误吗?什么?
  • 你想在这个声明中做什么? - Job.is_active.
  • @Bartek Gładys 一个错误,因为它将我引导到一个不存在的页面。这就是为什么我想重定向到 root_path。此外,我在控制台中看到is_active 仍然是假的,而不是真的
  • @vyom is_active 是 Job Model 的一个参数。 => 我创建了一个工作 => 然后我被重定向到付款,在我支付后它应该将刚刚创建的工作的 is_active 参数设置为 true => 然后它应该重定向到 root_pah
  • @Gugubaight 我认为您应该像 object.is_active 一样访问它,为什么要像 Class.is_active 一样称呼它?

标签: ruby-on-rails ruby ruby-on-rails-4 controller rubygems


【解决方案1】:

将保存的作业 id 作为参数发送到subscriberjobs/new。您可以在subscriberjobs/new html 表单中保留具有值job_id 的隐藏字段,这将调用您的SubscriberjobsController#update 方法。使用参数访问它。

在 JobController #create

redirect_to "/subscriberjobs/new?job_id=#{@job.id}"

在您的 SubScribeJob 表单中

hidden_field_tag 'job_id', params[:job_id]

在您的 SubScribeJobCotroller 中

@job = Job.find(params[:job_id])

【讨论】:

  • 提前感谢您的解释!我不知何故收到此错误:undefined method merge' for nil:NilClass` 用于表单行 (= f.hidden_field 'job_id', params[:job_id])。从来没有过。什么是合并?
  • 嗯...我得到了正确的网址:http://localhost:3000/subscriberjobs/new?job_id=2,但更新后出现此错误:ActiveRecord::RecordNotFound in SubscriberjobsController#update Couldn't find Job with 'id'=,它会将我引导至此页面:http://localhost:3000/subscriberjobs/1is_active 的值是还是false
猜你喜欢
  • 2016-06-11
  • 1970-01-01
  • 2021-11-15
  • 2015-10-07
  • 2014-03-02
  • 1970-01-01
  • 1970-01-01
  • 2015-02-19
  • 1970-01-01
相关资源
最近更新 更多