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");
6
  • 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. Commented Jun 27, 2015 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... Commented Jun 27, 2015 at 11:11
  • What you want is form validation with JavaScript Commented Jun 27, 2015 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 Commented Jun 27, 2015 at 11:19
  • Try changing your line to format.js { render :js => "my_function();" }. See this question for more details. Commented Jun 27, 2015 at 11:19

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

But as others have said adding client side validations are the way to go. This still requires a round trip to the sever.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.