Rails - page not loading after form submit via ajax

I have a form where I am using ajax validation as I made the form as: remote => true and the view is set as

<%= nested_form_for @interview_round, :remote => true do |f| %>
   <div id="error-div"> </div>
<%= f.text_field :interviewer_name %>
<%= f.text_field :log_skill, :id=>'hint-log' %>
<%= f.text_field :comm_skill, :id=>'hint-comm' %>
<%= f.submit %>
  <% end %>

      

and the controller is set as

  def update
    @interview_round = InterviewRound.where(id: params[:id]).first
    respond_to do |format|
     if  @interview_round.update_attributes round_params

        format.html{ redirect_to interview_path(@interview_round.interview_id), notice: I18n.t('round_created')}
        format.js
     else
       format.html { render :action => "edit" }
       format.js
     end
      end
  end

      

I am showing errors in update.js.erb file

<% if @interview_round.errors.present?%>
$('#error-div').html('<%= j(render 'shared/error_messages', :target => @interview_round) %>');
<% else %>
$('form.edit_interview_round').removeAttr('data-remote')
<% end %>

      

Now when I submit the form, errors arrive remotely via ajax and are displayed on the page, but when I submit the form without any error, the page does not redirect to the desired page, but stores the data in the database and submits the form, although I am also trying to delete deleted data in the form when no errors it removes in HTML but still the page doesn't load when i submit

Please, help!!

+3


source to share


2 answers


When you use remote: true, you cannot redirect via a "redirect" function like a "script" request, nor does it create a new page / headers, etc.

Next will work, for example, the syntax is ugly, but this will be redirected:

format.html{ redirect_to interview_path(@interview_round.interview_id), notice: I18n.t('round_created')}
format.js { render js: "window.location.href = '#{interview_path(@interview_round.interview_id)}'" }

      

However, you'd be better off making the JS code in a separate .js view file for the action. If you create a view file in the same directory as your HTML view called update.js, with javascript redirects there, you could remove the line I gave you and keep the relevant code where it belongs in the views.

To do this, you must create an "update.js.erb" file in app / views / {controllername} /update.js.erb with this:



<% if @interview_round.errors.present?%>
  $('#error-div').html('<%= j(render 'shared/error_messages', :target => @interview_round) %>');
<% else %>
  window.location.href = '<%= interview_path(@interview_round.interview_id) %>';
<% end %>

      

Then return the controller action back:

format.html{ redirect_to interview_path(@interview_round.interview_id), notice: I18n.t('round_created')}
format.js {}

      

The JS file will be rendered automatically by clearing out the controller.

+2


source


In the part else

in the update.js.erb file, you are not redirecting the user to another location, so whenever there are no errors in your form, your url will remain the same and you will not be redirected.



0


source







All Articles