简体   繁体   中英

Submitting two different forms with onebutton, in rails?

So I have a ticketing system I'm building with Ruby on Rails... I have two models i need to update with one button ... I have a ticket model and the comment model (comments belong to tickets, so association is fine).

Is there a way to do this on rails, or do I have to do it in Javascript/Jquery/AJAX-y way?

Thank You!

You can do like this

$("#button_id").on("click", function(){
  $("#form1").submit();
  $("#form2").submit();
});

Using nested attributes is a solution

define your association

class Ticket
    has_one :comment
    accepts_nested_attributes_for :comment
    def comment
        super || Comment.new
    end

end

then include fields for comments in your ticket form itself

f.fields_for :comment do |coment_field|
    comment_field.text_area :comment_text

This way, saving comment and ticket is tightly coupled.

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