简体   繁体   English

Ruby on Rails:将控制器中的请求参数传递给模型的after_create事件

[英]Ruby on Rails: Pass request params in controller to model's after_create event

I have two models: 我有两个模型:

1) Procedure
     title
     document_number

2) Revision
     revision_number

In my procedure model, I defined a method after_create to automatically create a Revision with revision_number set to 0.1. 在我的过程模型中,我定义了after_create方法来自动创建一个Revsion_number设置为0.1的Revision。

# app/models/procedure.rb

# How do I get revision_number from the request params in this method?
after_create do |procedure|
    procedure.revisions.create(revision_number: 0.1)
end

That works well, however, I do not want to always default to 0.1 - For example, if I want to specify a revision number from parameters sent in the form request to the controller, I am a bit lost there. 效果很好,但是,我不想总是默认为0.1-例如,如果我想从表单请求中发送给控制器的参数中指定修订号,那我就有点迷失了。 I looked throughout the guides and I wasn't able to find a way to pass data into the create event. 我浏览了所有指南,但找不到找到将数据传递到create事件的方法。 This is my method in my controller: 这是我在控制器中的方法:

# app/controllers/procedures_controller.rb

def create
    @procedure = current_user.procedures.build(procedure_params)

    if @procedure.save
        render json: @procedure, status: :created, location: @procedure
    else
        render json: @procedure.errors, status: :unprocessable_entity
    end
end

def procedure_params
    params.require(:procedure).permit(:title, :document_number, :revision_number)
end

The only way I can think of accomplishing this is by adding a column 'revision_number' to my procedure model, and then checking whether it is set in my 'after_create' method. 我能想到的唯一方法是在过程模型中添加一列“ revision_number”,然后检查是否在“ after_create”方法中对其进行了设置。

I would appreciate any other suggestions for how I might be able to tackle this problem. 对于可能如何解决此问题的任何其他建议,我将不胜感激。 Or if there is already a way and I missed it in the guides, I would appreciate a reference to that section. 或者,如果已经有一种方法,但我在指南中错过了,请参考该部分。 Thanks! 谢谢!

you are trying to violate MVC architecture and Single Responsiblity Principle . 您正在尝试违反MVC architectureSingle Responsiblity Principle Controller is responsible for handling HTTP request and Model is responsible for interacting with database. Controller负责handling HTTP requestModel负责interacting with database.进行interacting with database.

In any case you commited to violate rules :- 无论如何,您承诺违反规则:

class Procedure < ActiveRecord::Base
  def add_revision_number(number)
    self.revision_number = number
  end
end

class ProceduresController < ApplicationController
  def create
    ...
    @procedure.add_revision_number(params[:revision_number])
    ...
  end
end

now access revision_number in model 现在访问revision_number模型

I prefer not to pass controller request object directly. 我不想直接传递控制器request对象。 It Isolates model from current request. 它将模型与当前请求隔离。

app/models/procedure.rb app / models / procedure.rb

attr_accessor :revision_no

after_create do |procedure|
    procedure.revisions.create(revision_number: self.revision_no)
end

Now you can pass value in revision_no. 现在,您可以在version_no中传递值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM