Tiamon
Tiamon

Reputation: 237

Redirecting Rails form_tag to a modal

In my rails app I have a form_tag which I want to be redirected to a modal when submitted. I have the following for the form_tag but the problem is the modal pops-up when i click on the fields of the form.

<%= form_tag generate_report_path, 
      {method: :get, remote: true, 
         'data-toggle' =>  "modal", 'data-target' => '#modal-window'} do %>

my geneate_report method

def generate_report
  ####code to get relevant student_ids
  @students = Student.where(id: @student_ids)
  respond_to do |format|
    format.js {render 'reports/show_report'}
  end
end

Upvotes: 1

Views: 1374

Answers (1)

Ken Stipek
Ken Stipek

Reputation: 1522

If you want the modal to pop up when the submit button is pressed simply move

'data-toggle' => "modal", 'data-target' => '#modal-window'

to your button_tag.

I think you might actually want to have the modal open when the user receives a response from the server after submitting the form. If so, simply manually open the modal in your create.js.erb file (the view being rendered by the remote form submit).

$('#modal-window').modal('show');

Upvotes: 3

Related Questions