【问题标题】:Update method doesn't get the form input from edit method更新方法没有从编辑方法获取表单输入
【发布时间】:2014-07-30 07:21:45
【问题描述】:

我正在尝试创建和更新合同。 create 工作正常。但是我的更新采用默认值而不是我编辑的值。为什么?我的结束日期始终是开始日期的下一年。

 before_filter :only => :create do
   @contract = Contract.new(params[:contract])
     if @contract.start_date.present?
       n = @contract.num_years
       n = n.blank? ? 1 : n.to_i
       @contract.end_date = @contract.start_date + n.years - 1.day
     end
   @contract.save!
   Mailers.delay(queue: "Mailers").contract_created_notification(@contract)
 end
 controller do
   def update
    @contract = Contract.find(params[:id])
     if @contract.start_date.present?
       n = @contract.num_years
       n = n.blank? ? 1 : n.to_i
       @contract.end_date = @contract.start_date + n.years - 1.day
       update!
       Mailers.delay(queue: "Mailers").contract_created_notification(@contract)
     end
   end

日志文件:

插入delayed_jobs (attempts, created_at, failed_at, handler, last_error, locked_at, locked_by, priority, @98765433@2, @98765433@2, @98765433@2 987654334@) 值 (0, '2014-07-30 06:41:57', NULL, '--- !ruby/object:Delayed::PerformableMailer\nobject: !ruby/class \'Mailers\'\nmethod_name: :contract_created_notification\nargs:\n- !ruby/ActiveRecord:Contract\n 属性:\n id: 464\n school_id: 370\n start_date: 2014-07-01\n end_date: 2022-06-30\n num_students: 900\n pymt_status: false\n created_at: &1 2014-07-30 06:41:57.876186386 Z\n updated_at: *1\n', NULL, NULL, NULL, 0, 'Mailers', '2014-07-30 06:41:57', '2014-07-30 06:41:57')

> Parameters: {"utf8"=>"✓", "authenticity_token"=>"F7W4ccjQe8wk8Z9VZB18efJBhHp6LPCtifC6PDYVEF0=", "contract"=>{"start_date(1i)"=>"2014", "start_date(2i)"=>"7", "start_date(3i)"=>"1", "num_years"=>"5", "num_students"=>"100", "pymt_status"=>"0"}, "commit"=>"Update Contract", "id"=>"465"}

 INSERT INTO `delayed_jobs` (`attempts`, `created_at`, `failed_at`, `handler`, `last_error`, `locked_at`, `locked_by`, `priority`, `queue`, `run_at`, `updated_at`) VALUES (0, '2014-07-30 08:23:24', NULL, '--- !ruby/object:Delayed::PerformableMailer\nobject: !ruby/class \'Mailers\'\nmethod_name: :contract_notification\nargs:\n- !ruby/ActiveRecord:Contract\n attributes:\n id: 465\n school_id: 370\n start_date: 2014-07-01\n end_date: 2015-06-30\n num_students: 100\n pymt_status: false\n created_at: 2014-07-30 08:04:22.000000000 Z\n updated_at: 2014-07-30 08:23:24.782557418 Z\n', NULL, NULL, NULL, 0, 'Mailers', '2014-07-30 08:23:24', '2014-07-30 08:23:24')

【问题讨论】:

  • {a} 我看到了 create 操作的日志,但是您能否也添加 update 操作的日志,这表明了问题? {b} 您的日志还应该包含要发布的参数,例如 Parameters: {"utf8"=>"✓", "topic"=>"General", ... } - 来自创建和更新重复的这一行也会很有帮助。
  • 感谢您的回答。我从更新中添加我的日志

标签: ruby-on-rails ruby


【解决方案1】:

奇怪的控制器代码

我的注意力最初是在这里引起的:

before_filter :only => :create do

controller do

这些到底是什么?它们看起来不像典型的 Rails 控制器代码:

--

#app/controllers/contracts_controller.rb
Class ContractsController < ApplicationController
   before_action :set_vars, only: [:edit, :update]

   def new
      @contract = Contract.new
   end

   def create
      @contract = Contract.new(contract_params)
      @contract.set_date if @contract.start_date.present?
      @contract.save
   end

   def update
      @contract.set_date if @contract.start_date.present?
      @contract.update(contract_params)
   end

   private

   def set_vars
       @contract = Contract.find params[:id]
   end

   def set_date
       n = self.num_years
       n = n.blank? ? 1 : n.to_i
       self.end_date = self.start_date + n.years - 1.day
   end

   def contract_params
      params.require(:contract).permit(:contract, :attributes)
   end
end

日期

虽然我不确定如何解决您的问题,但我认为对控制器进行排序将是第一步。

如果您想根据num_years 属性设置end_date,我建议您必须考虑edit 方法传递num_years 属性,然后您可以通过到update 操作

【讨论】:

  • 感谢您的回答。但我正在使用活动管理员。我怎样才能根据那个改变我的编码?
猜你喜欢
  • 2023-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-09
  • 1970-01-01
  • 1970-01-01
  • 2018-10-20
  • 1970-01-01
相关资源
最近更新 更多