如果url params存在则更新数据

时间:2017-10-16 07:44:22

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

如果datetime columns,我正在尝试更新一些url params exist,以便稍后发送电子邮件通知。列是first_expiration ,second_expiration, third_expiration内的accounts table。在网址中,我传递了几个参数:当前注册用户emailplan_id

这是设计registrations_controller中的设置:

修订后的行动:

after_action :set_expiration, only: [:create]

def set_expiration
  if params.has_key?["plan_id"].present? == "nt127"
    @first_expiration = DateTime.now + 152.days
    @second_expiration = DateTime.now + 167.days
    @third_expiration = DateTime.now + 177.days
    current_client.account.update!(first_expiration: @first_expiration,
                                           second_expiration: @second_expiration,
                                           third_expiraton: @third_expiration)
  elsif params.has_key?["plan_id"].present? == "mli90"
    @first_expiration = DateTime.now + 335.days
    @second_expiration = DateTime.now + 350.days
    @third_expiration = DateTime.now + 360.days
    current_client.account.update!(first_expiration: @first_expiration,
                                         second_expiration: @second_expiration,
                                         third_expiraton: @third_expiration)
  end
end

def current_client
   if cookies["email"].present?
    User.find_by_email(cookies["email"])
   end
end

但是,如果我检查控制台,则更新无效。没有错误弹出,通常没有任何更新。

更新1

这是我的网址:

http://localhost:3000/transactions/new?email=test11%40gmail.com&plan_id=ntl127&price=150.0

我正在通过这样的参数:

<%= hidden_field_tag(:email, params["email"]) %>
<%= hidden_field_tag(:plan_id, params["plan_id"]) %>
<%= hidden_field_tag(:amount, params["price"]) %>

3 个答案:

答案 0 :(得分:0)

编辑此内容 elsif params["plan_id"] == "mli90"

答案 1 :(得分:0)

我建议您使用.update!代替.update_attributes!

答案 2 :(得分:0)

您已将after_actionset_expiration添加到RegistrationsController

但如果您遵循常见的Rails惯用法,则以/transactions/new开头的示例网址将指向TransactionsController

只需将方法和after_action移至您的TransactionsController,即可。