Home respond_to with format.js only works on Flexirest exception
Reply: 0

respond_to with format.js only works on Flexirest exception

user1721
1#
user1721 Published in August 15, 2018, 5:36 am

I have this at my controller:

def create
    chef_params = params[:chef].except :city, :type

    begin
      Chef.application(chef_params)
    rescue Flexirest::HTTPClientException, Flexirest::HTTPServerException => e
      if e.status == 422
        invalid_fields = []
        e.result.each{|field| invalid_fields << field }

        @error_msg = "Invalid values at #{invalid_fields.join(', ')}"
      else
        @error_msg = 'The service is unavailable, please try again later.'
      end
      @error = true
    end

    respond_to do |format|
      format.js { render 'chefs/create'}
    end
  end

And this is the create.js.erb file that I'm hoping the respond_to will render:

var submit = $('#be-an-expert-form input[type=submit]');
var error = $('#be-an-expert-form .error');

if(<%= @error %>) {
  $(submit).prop('value','Submit');
  $(submit).attr('disabled', false);
  $(error).html("<%= @error_msg %>")
} else {
  $(submit).prop('value','Thanks! We’ll be in touch.');
}

Right now it only gets rendered if the Chef.application(chef_params) fails and falls into the rescue.

Otherwise, the request on the controller ends with 200 but it doesn't render the js.

method=POST path=/experts format=js controller=chefs action=create status=200 duration=155.03 view=26.06

I know it's going inside of the respond_to but it never gets to the js.

This is my form:

= form_for :chef, url: '/experts', action: "create", remote: true, html: {id: "be-an-expert-form" } do |f|
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.288981 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO