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.
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.
Comments