TEJINDER SINGH
TEJINDER SINGH

Reputation: 89

Rails 7: Forms having two buttons (One for GET request and One for POST request)

I am working on Ruby on Rails application and have designed a form.

I have two buttons in the form.

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.

Upvotes: 1

Views: 537

Answers (1)

Alex
Alex

Reputation: 30111

Have as many buttons as you need:

<%= f.submit "Generate",
  formmethod: :get,
  formaction: home_homepage_path
%>

<%= f.submit "Save",
  formmethod: :post,
  formaction: home_save_plot_path
%>

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/submit#additional_attributes


Perhaps less homes in your routes could be better:

# config/routes.rb
Rails.application.routes.draw do
  root "home#show"

  resources :plots, only: [:create, :show] do
    new do          # you're making a new plot
      get :preview  # you want to take a peek
    end
  end
end
# $ bin/rails routes -g "home|plot"
#
#           Prefix Verb URI Pattern                  Controller#Action
#             root GET  /                            home#show
# preview_new_plot GET  /plots/new/preview(.:format) plots#preview
#            plots POST /plots(.:format)             plots#create
#             plot GET  /plots/:id(.:format)         plots#show
# app/controllers/home_controller.rb
class HomeController < ApplicationController
  def show
  end
end
# app/views/home/show.html.erb

<%= form_with model: Plot.new do |f| %>
  <%= f.text_field :name %>

  <%= f.submit "Generate",
    formmethod: :get,                   # get it, don't post
    formaction: preview_new_plot_path,  # any path works, `home_homepage_path`
    data: {turbo_frame: :plot_preview}  # but get a frame only
  %>

  <%= f.submit "Save" %>
<% end %>

<%= turbo_frame_tag :plot_preview do %>
  hit generate to see plot
<% end %>

Now, maybe you don't have a Plot model, but making plots sounds like a PlotsController job:

bin/rails g scaffold_controller Plot

Add preview action:

# app/controllers/plots_controller.rb
class PlotsController < ApplicationController
  # GET /plots/new
  def new
    @plot = Plot.new
  end

  # GET /plots/new/preview
  def preview
    @plot = Plot.new(plot_params)
  end

  # POST /plots
  def create
    @plot = Plot.new(plot_params)
    respond_to do |format|
      if @plot.save
        format.html { redirect_to plot_url(@plot), notice: "Created." }
      else
        format.html { render :new, status: :unprocessable_entity }
      end
    end
  end

  private

  def plot_params
    params.fetch(:plot, {}).permit(:name)
  end
end

When you hit generate, plot_preview frame will be updated with:

# app/views/plots/preview.html.erb
<%= turbo_frame_tag :plot_preview do %>
  <%= @plot.name %>
<% end %>

Upvotes: 0

Related Questions