I have two buttons that both work independently below. One button creates a relationship between the user and the step via ajax. Another button executes a function, that chooses which bootstrap modal to display(success or failure). I need the functionality of both buttons in one single button, as opposed to two separate buttons. However, for example, when I try to add class data-toggle="modal" to the the ajax button, the button no longer is capable of creating a relationship and the data-modal does not work.
Here is a working button, of which functionality I am trying to replicate:
<div data-toggle="modal">
<%= link_to_function "check answer", 'execute();', class: "btn btn-success"%>
</div>
Here is the execute(); function from above, which isn't all together relevant other than it takes true or false as an argument and shows the appropriate modal based upon which.
if(checked)
{
//display correct-answer dialogue
$('#modal-simple-success').modal('show')
}
else
{
//display wrong-answer dialogue
$('#modal-simple-failure').modal('show')
}
I need the functionality of above to work for the below button - basically the button below renders an ajax button that creates or removes a relationship. When I try to add data-toggle="modal" to this, it no longer builds the relationship and the ajax doesn't work. I need all three functionalities to work(build the relationship[below], initiate execute function [above], and then display the modal [above].
<div id="attempt_step">
<% if current_user.attempted_step?(@step) %>
<%= render 'remove_attempt' %>
<% else %>
<%= render 'attempt_step' %>
<% end %>
</div>
Here is the specific form for ajax for the create relationship:
<div class="checkAnswer">
<%= form_for(current_user.user_steps.build(step_id: @step.id), remote: true) do |f| %>
<div><%= f.hidden_field :step_id %></div>
<%= f.submit "check answer", class: " btn btn-large btn-primary" %>
<% end %>
</div>
Here is the "create" javascript for ajax
$("#attempt_step").html("<%= escape_javascript(render('steps/attempt_step')) %>")
Here is an example idea of what I am looking for, but does not work:
<div id="attempt_step" data-toggle="modal">
<%= link_to_function "check answer", 'execute();' do %>
<% if current_user.attempted_step?(@step) %>
<%= render 'remove_attempt' %>
<% else %>
<%= render 'attempt_step' %>
<% end %>
<% end %>
</div>
How can I achieve the functionality of both buttons (ie making the above work)?