简体   繁体   中英

Rails - page is not loading after submitting form through ajax

I have a form in which i am using ajax validation, for that i have made the form as :remote => true and the view is given 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 given 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 the errors in the update.js.erb

<% 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 am submitting form the errors are coming remotely through ajax and showing on the page but when i am submitting the form without any error the page is not redirecting to the desired page but it is storing the data in database and submitting the form , though i am also trying to remove the data-remote on the form when there are no error it is removing in the HTML but still the page is not loading when i submit

Please Help !!

When you use remote: true you can't redirect via the 'redirect' function as it's a 'script' request and doesn't create a new page/headers etc.

The following would work for example, the syntax is ugly but this will redirect:

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 would be better off doing the JS code within a seperate .js view file for the action. If you create a view file in the same directory as your HTML view called update.js, with the javascript re-direct in there, you could then remove the line I gave you altogether and keep view related code where it belongs, in the views.

To do that, you would create a file 'update.js.erb' 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 revert the controller action back to:

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

The JS file will be rendered automatically, cleaning up the controller.

在update.js.erb的else部分中,您没有将用户重定向到其他位置,因此只要表单中没有错误,您的网址就会保持不变,并且您不会被重定向。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM