Rails How to pass params from controller to after_save inside model

前端 未结 3 1385
有刺的猬
有刺的猬 2021-01-06 08:10

I have a Rfq contoller i am creating new or updating existing Rfqs, when i create or update the object is saved, what i want is as i have number of quotes params i want to u

相关标签:
3条回答
  • 2021-01-06 08:43

    I think you want to be able to have 1 form that saves both the main object and all of the child objects. If not, disregard.

    In rails, this is named "nested_attributes"

    you'll add this to your model:

    accepts_nested_attributes_for :quotes
    # assuming you have 
    has_many :quotes
    

    and then in your form view:

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

    Check this out at Ryan's Blog: Nested Attributes

    0 讨论(0)
  • 2021-01-06 08:47

    Honestly if it deals with params, it's probably a good idea to put that type of logic in the controller, lest you muddle the responsibilities of the model and controller.

    That is, in the controller:

    if @foo.save
      # Update line_items using params[:quotes]
    end
    0 讨论(0)
  • 2021-01-06 08:57

    If you're trying to use the params hash in your model, you are violating principles of MVC. The model should stand alone with arguments. If you are trying to do the following:

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

    You should do the following instead:

    # controller
    Model.foo(params[:bar])
    
    # model
    def foo(foobar)
      foobar.reverse!
    end
    
    0 讨论(0)
提交回复
热议问题