0

I am trying to render a partial upon change in the drop down list. There is the onchange javascript function which directs to a link to display the corresponding form.

But here I am getting a #<ActionController::UnknownFormat: ActionController::UnknownFormat> error inside the get_template method in controller.

I suppose it is something to do with calling the link through javascript, as the request is processed as HTML.

Processing by XYZController#get_template as HTML

How to process it as JS ?

Here's the detailed code.

dropdown.html.erb

<div id="requests_dropdown">
Choose the type of request : <%= select_tag 'drop_request_id', options_for_select(@request_types.map{|x| [x[:name], x[:id]] } ) %>
</div>

Javascript

<script>

$('#drop_request_id').on('change', function() { 
  var request_type_id = $('#drop_request_id').val();
  var href =  'get_template/' + request_type_id ;
  window.location = href;
});
</script>

controller

   def get_template

    @request_type = [x,y,z] 
    respond_to do |format|
            format.js
    end
end

get_template.js.erb

$("#request_form_partial").html("<%= escape_javascript(render partial: 'request_form', locals: {  request_type: @request_type } ) %>"); 
1
  • Out of curiosity, why are you using window.location = href instead of an AJAX get call? Commented Jul 6, 2018 at 20:27

1 Answer 1

1

You need to call your method via ajax. You are getting error because you are trying to GET the html format,whereas your method renders the js format response.

Please edit your code to following:

     <script> $('#drop_request_id').on('change', function() { 
var request_type_id = $('#drop_request_id').val(); 
var href = 'get_template.js/' + request_type_id ; 
$.get(href);
}); 
</script>
Sign up to request clarification or add additional context in comments.

Comments

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.