How to set the same value for two inputs for two models in a Rails form

attentionjay

I have an app where I need to take simple user input and update two models with the one value. I have been researching this for so many days now and it is time to get some help. I am using Rails 4.1. I settled on using a form_for helper with a fields_for nested in it based on the answers to many similar questions. That covers the two models, but I am still left with the problem of getting the value correct for each model. The form only has one input and I need to capture that value for both models. It works for updating one model, but the updated value does not work for the other model. The form is very simple. I am updating the :qtydemanded field on the Plan model, but I would like the same value to update the :value field on the Chart model. I tried to set value => @product.qtydemanded on the Chart model, but that set the :value field equal to the existing qtydemanded in the Plan model before the update. It did not capture the updated value from the form for the Chart model.

<%= form_for @product, url: update_plan_path do |p| %>
<table>
  <tr>
    <td>Sku Number:</td>
    <td><%= p.text_field :sku_number, :readonly => true %></td>
 </tr>
 <tr>
    <td>Period:</td>
    <td><%= p.text_field :period, :readonly => true %></td>
</tr>
<tr>
   <td>Qtydemanded:</td>
   <td><%= p.text_field :qtydemanded %></td>
 <%=fields_for @chart do |chart| %>
   <td><%= chart.hidden_field :value %></td>
 <% end %>
</tr>
<tr>
  <td><%= p.submit "Save" %></td>
</tr>
</table>
<% end %> 

The controller is also very simple. I am not doing anything super complicated there. I am simply calling the update_attributes on the strong params. The update is working. It just does not have the correct value. When I submit the form the Chart :value field is updated with the old qtydemanded value from the Plan model.

def update_plan
    @product = Plan.where('sku_number = ? and period = ?', params[:sku_number],params[:period]).first
    @chart = Chart.where('sku_number = ? and period = ? and feature = "qtydemanded"',params[:sku_number],params[:period]).first
    if @product.update_attributes(plan_params)
        @chart.update_attributes(chart_params)
        redirect_to(:action => 'show_product', :sku_number => @product.sku_number)
        else
        puts "There is an error"
    end
end

private

def chart_params
    params.require(:chart).permit(:value)
end

The route is also simple in that it just passes the sku_number and period to the controller to find the correct record.

patch 'products/:sku_number/:period/update_plan' => 'products#update_plan', :as => :update_plan

I can see that the form is being interpreted correctly and it is putting the input values into what looks to me like an array such as plan[qtydemanded] and chart[value]. I tried to grab that value in the controller like this: @chart.update(:value => plan[qtydemanded]). That returned :value as null. I think the answer is close to that, but I can't figure it out. I have tried countless variations of other answers and I am out of ideas. I sincerely appreciate any help.

railsdog

Does

if @product.update_attributes(plan_params)
  chart_params[:value] = plan_params[:qtydemanded]
  @chart.update_attributes(chart_params)

solve your issue?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails: How to create an object that belongs to two models

From Dev

How to use two separate models in the same view

From Dev

Bootstrap 3: How to get two form inputs on one line and other inputs on individual lines?

From Dev

How to set two local variables with the same value in sql server?

From Dev

Rails search in two models

From Dev

Binding two inputs to the same model

From Dev

Differentiate tag for two models in Rails

From Dev

Edit two models in one form

From Dev

How to accept attributes for two models in one form?

From Dev

SQLAlchemy how to define two models for the same table

From Dev

Form with two inputs not submitting?

From Dev

How to combine two inputs into one value and bind?

From Dev

Two sets of JAXB models sharing the same set of interfaces

From Dev

Rails - one form to two models

From Dev

Same db table for two models

From Dev

How to join two models?

From Dev

How do I set up a has many through for two different associations between the the same models

From Dev

How to validate the uniqueness of attribute in two models in Rails?

From Dev

How to set attributes for form field rendered by two inputs in Symfony2

From Dev

Two inputs with the same name

From Dev

two select menus with the same parameters in a form rails

From Dev

Two buttons into same form

From Dev

Set Same value in two dropdowns by click on checkbox

From Dev

Two inputs for the same value in Shiny

From Dev

ruby on rails - Ordering two models

From Dev

How to create an association between two models with form choice field using Active Admin, Paperclip in Rails 5.0.1

From Dev

How to create multiple pivot tables for the same two models in laravel?

From Dev

Rails 5, How to name two models with an associated table?

From Dev

Rails: How to merge two hashes if a specific key has the same value?

Related Related

  1. 1

    Rails: How to create an object that belongs to two models

  2. 2

    How to use two separate models in the same view

  3. 3

    Bootstrap 3: How to get two form inputs on one line and other inputs on individual lines?

  4. 4

    How to set two local variables with the same value in sql server?

  5. 5

    Rails search in two models

  6. 6

    Binding two inputs to the same model

  7. 7

    Differentiate tag for two models in Rails

  8. 8

    Edit two models in one form

  9. 9

    How to accept attributes for two models in one form?

  10. 10

    SQLAlchemy how to define two models for the same table

  11. 11

    Form with two inputs not submitting?

  12. 12

    How to combine two inputs into one value and bind?

  13. 13

    Two sets of JAXB models sharing the same set of interfaces

  14. 14

    Rails - one form to two models

  15. 15

    Same db table for two models

  16. 16

    How to join two models?

  17. 17

    How do I set up a has many through for two different associations between the the same models

  18. 18

    How to validate the uniqueness of attribute in two models in Rails?

  19. 19

    How to set attributes for form field rendered by two inputs in Symfony2

  20. 20

    Two inputs with the same name

  21. 21

    two select menus with the same parameters in a form rails

  22. 22

    Two buttons into same form

  23. 23

    Set Same value in two dropdowns by click on checkbox

  24. 24

    Two inputs for the same value in Shiny

  25. 25

    ruby on rails - Ordering two models

  26. 26

    How to create an association between two models with form choice field using Active Admin, Paperclip in Rails 5.0.1

  27. 27

    How to create multiple pivot tables for the same two models in laravel?

  28. 28

    Rails 5, How to name two models with an associated table?

  29. 29

    Rails: How to merge two hashes if a specific key has the same value?

HotTag

Archive