Rails How to pass parameters from the controller to after_save inside the model

I have an rfq contoller. I am creating a new or updating existing Rfq when I create or update an object I want, since I have a number of quotation mark options. I want to update the line_items table with quotes in the [: citations] parameters in the quote_price column after saving Rfqs

I know this is confusing, but whoever has ror-ish you should have some hint I want to ask.

+4
source share
3 answers

If you try to use the params hash in your model, you violate the principles of MVC. The model should be alone with the arguments. If you are trying to do the following:

 # controller Model.foo # model def foo params[:bar].reverse! end 

Instead, you should do the following:

 # controller Model.foo(params[:bar]) # model def foo(foobar) foobar.reverse! end 
+11
source

Honestly, when it comes to params , it's probably a good idea to put this type of logic in the controller so that you don't get confused with the responsibilities of the model and the controller.

That is, in the controller :

 if @foo.save # Update line_items using params[:quotes] end 
+2
source

I think you want to have 1 form that saves both the main object and all child objects. If not, do not pay attention.

In rails, this is called "nested_attributes"

you add this to your model:

 accepts_nested_attributes_for :quotes # assuming you have has_many :quotes 

and then in the form of a form:

 <% form.fields_for :quotes do |child_form| %> <%= child_form.text_field :name %> <% end %> 

Check it out on the Ryan blog: Nested Attributes

+1
source

Source: https://habr.com/ru/post/1335997/


All Articles