I am working on Ruby on Rails application and have designed a form.
I have two buttons in the form.
- Generate Plot button which initiates a GET request
- Save button which initiates a POST request.
The GET request is working alright and I am able to do an operation on the parameters value straightforwardly by saving params values in instance variables in the controller.
However, I also want the form to save the form parameters to a model when SAVE Button is clicked.
I am struggling to do this, and I have tried some ways, but I am not sure if my thought process is correct.
Below is the form code:
Form:
<%= form_with url: home_homepage_path, method: :get do |f| %>
<div class="row">
<div class="form-group col-6">
<b>LOWER SPECIFICATION LIMIT (LSL):</b>
<%= f.number_field :lower_specification_limit, placeholder: "Enter lower specification limit", class: 'form-content form-content w-100 p-1' %>
</div>
<div class="form-group col-6">
<b>UPPER SPECIFICATION LIMIT (USL):</b>
<%= f.number_field :upper_specification_limit, placeholder: "Enter upper specification limit", class: 'form-content form-content w-100 p-1' %>
</div>
</div>
<div class="row">
<div class="form-group col-12">
<b>PROCESS TIME:</b>
<%= f.text_field :process_time_array, multiple: true, placeholder: "Enter process time. This value could be an array.", class: 'form-content form-content w-100 p-1' %>
</div>
</div>
<br/>
<div class="actions">
<%= f.submit 'Generate Plot', class: 'btn btn-block btn-outline-dark rounded-0' %>
<%= link_to "SAVE", home_save_plot_path, method: :post , class: 'btn btn-block btn-outline-dark rounded-0' %>
</div>
<% end %>
I am trying to pass these form parameters to a method in controller to save the data into the Operation model. I also have defined a method in the Controller as shown below.
def save_plot
@operation = Operation.new
@operation = Operation.new(params[:upper_specification_limit, :lower_specification_limit, :process_time_array])
@operation.save
end
I also have mentioned the route as below:
post 'home/save_plot'
Observation:
The parameters data is not getting saved into the form. Only "created_at" and "updated_at" are getting saved.