简体   繁体   中英

Ruby Rails submit two forms in one click

I have these forms:

<%= form_for(@user) do |f| %>
  <div>
    <%= f.number_field :money, :value => @user.money %>
  </div>
<% end %>

and

<%= form_for @product, :url => product_path, :html => { :multipart => true }  do |f| %>
  <div>
    <%= f.label :count, 'How Many product?' %><br />
    <%= f.number_field :count, :value => "1" %>
  </div>
  <div>
    <%= f.submit('submit') %>
  </div>
<% end %>

is there any way to submit this two at once when clicking submit button ? Thanks!

A service object might be a good way to approach this.

class Order
  include ActiveModel::Model

  attr_accessor :money, :count

  def initialize(user=nil, product=nil)
    @user = user
    @product = product
    @money = user.money
    @count = 1
  end

  def persisted?
    false
  end

  def save
    // this code needs to save to db
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM