0

I need to render a javascript file to display an alert box when the user doesn't fill out any part of the form. The form will not save until all the inputs are filled. I tried many different combinations and I feel like I'm further from a solution.

def create
  @question = Question.new(question_params)
    if @question.save
      redirect_to new_question_share_quiz_path(@question)
    else
    respond_to do |format|
      format.js { render :action => "try" }
    end
  end

end

And the try.js file is in the views folder.

The try.js file only has one line of code

alert("You need to fill out the whole form");
Rick G
  • 53
  • 1
  • 9
  • 2
    Do you have to use JavaScript to tell them there's an error? I usually use a message with like `flash[:warning]` and then render the edit page again. – nicholas79171 Jun 27 '15 at 11:09
  • Yeah it needs to be an alert box. It's the way that the page is styled that requires it; my designer wants it that way as well... – Rick G Jun 27 '15 at 11:11
  • What you want is form validation with JavaScript – Pavan Jun 27 '15 at 11:18
  • why would you want it to even hit create method in controller? Why not check it once user clicks on submit button and send it only when fields are filled – Mandeep Jun 27 '15 at 11:19
  • Try changing your line to `format.js { render :js => "my_function();" }`. See [this question](http://stackoverflow.com/questions/16697333/how-to-call-javascript-functions-from-the-controller-in-rails) for more details. – nicholas79171 Jun 27 '15 at 11:19
  • The form has too many text inputs and checkboxes. My designer just wants an alert box to validate the whole form. – Rick G Jun 29 '15 at 09:15

1 Answers1

0

If you absolutely have to do it with an alert - this is one solution:

Create a flash message if the form is valid.

def create
  @question = Question.new(question_params)
  if @question.save
    redirect_to new_question_share_quiz_path(@question)
  else
    respond_to do |format|
      render :new, alert: 'The form is not valid'
    end
  end
end

Add flashes to your /views/layouts/application.html.erb.

<% if flash.any? %>
<ul id="flash">
  <% flash.each do |key,message| %>
  <li class="flash flash-<% key %>">message</li>
  <% end %>
</ul>
<% end %>

Then add use javascript to create alert if there is a "alert" level flash.

$('.flash-alert', $('#flash')).each(function(){
  alert($(this).text());
});
<!-- THIS IS JUST SO THAT YOU CAN RUN THE SNIPPET!! -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="flash">
  <li class="flash flash-notice">Should trigger an alert</li>
  <li class="flash flash-alert">Should trigger a alert</li>
</ul>

The advantage here is that this is the conventional way of handling form feedback in rails and when your designer comes to his senses and realizes it's just plain annoying to have to close a dialog before correcting the form you can just lop the javascript off.

max
  • 96,212
  • 14
  • 104
  • 165
  • But as others have said adding client side validations are the way to go. This still requires a round trip to the sever. – max Jun 27 '15 at 12:55