ruby Rails 7:表单有两个按钮(一个用于GET请求,一个用于POST请求)

e0bqpujr  于 2023-10-18  发布在  Ruby
关注(0)|答案(1)|浏览(135)

我正在开发Ruby on Rails应用程序,并设计了一个表单。
我有两个按钮的形式。

*Generate Plot按钮发起GET请求

  • 启动POST请求的保存按钮

GET请求工作正常,我可以直接通过在控制器的示例变量中保存参数值来对参数值进行操作。
但是,我还希望在单击“保存”按钮时,表单将表单参数保存到模型中。
我正在努力做到这一点,我也尝试过一些方法,但我不确定我的思维过程是否正确。
下面是表单代码:
形式:

<%= 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 %>

我试图将这些表单参数传递给控制器中的一个方法,以便将数据保存到操作模型中。我还在控制器中定义了一个方法,如下所示。

def save_plot
   
    @operation = Operation.new
    @operation = Operation.new(params[:upper_specification_limit, :lower_specification_limit, :process_time_array])
    @operation.save

  end

我也提到了路线如下:

post 'home/save_plot'

观察结果:
参数数据未保存到表单中。只有“created_at”和“updated_at”会被保存。

ohfgkhjo

ohfgkhjo1#

有你需要的任意多个按钮:

<%= 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*

也许你的路线上的房子越少越好:

# 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 %>

现在,也许你没有一个Plot模型,但是绘制图听起来像一个PlotsController工作:

bin/rails g scaffold_controller Plot

添加preview操作:

# 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

当你点击generate时,plot_preview帧将被更新为:

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

相关问题