How to make ajax call and show error messages
ruby-on-rails
Solution
First of all you should add a remote: true to your existing form to allow remote action. TODO this just add on the first line of your form the remote: true,
<%= form_for(@regist, remote: true) do |f| %>
the rest leave it as it is. Then you need to make your controller to respond to remote calls, therefore you need to alter the responds_to block of create action:
respond_to do |format|
if @regist.save
format.html { redirect_to @regist, notice: 'Regist was successfully created.' }
format.json { render json: @regist, status: :created, location: @regist }
format.js { render js: "window.location.href='"+regists_path+"'"}
else
format.html { render action: "new" }
format.json { render json: @regist.errors, status: :unprocessable_entity }
format.js
end
end
The last step you have to do is to add a file to your app/views/regists/ directory where you should add a create.js.erb file:
<% if @regist.errors.any? %>
$('#new_regist').effect('highlight', { color: '#FF0000'}, 1000); // for highlighting
// or add here whatever jquery response you want to have to your views.
<% end %>
You will get your validation errors displayed like before above the form. You have to add the redirect to your controller to the desirable action of your choice. I have added for you a window.location.href as a response to the regists_path.
Problem
Two of the action of My registration controller is new and create. ``` def new @regist = Regist.new respond_to do |format| format.html # new.html.erb format.json { render json: @regist } end end def create @regist = Regist.new(regist_params) respond_to do |format| if @regist.save format.html { redirect_to @regist, notice: 'Regist was successfully created.' } format.json { render json: @regist, status: :created, location: @regist } else format.html { render action: "new" } format.json { render json: @regist.errors, status: :unprocessable_entity } end end end ``` And the new form contain following code. ``` <%= form_for(@regist) do |f| %> <% if @regist.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@regist.errors.count, "error") %> prohibited this regist from being saved:</h2> <ul> <% @regist.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.collection_select :student_id, Student.all, :id, :name %><br /> </div> <div class="field"> <%= f.collection_select :semester_id, Semester.all, :id, :name %><br /> </div> <div class="field"> <% for subject in Subject.find(:all) %> <%= check_box_tag "regist[subject_ids][]", subject.id %> <%= subject.name %><br> <% end %> </div> <div class="field"> <%= f.label :date_of_birth %><br /> <%= f.text_field :date_of_birth %> </div> <div class="actions"> <%= f.submit %> </div> <% end %> ``` Now, when someone click on submit button I want to make ajax call using remote true if there are validation errors and show the errors without reloading the page. And if there are no validation errors I want the user to be redirected to show page. How can I do this?