J (render (@partial)) returns an error: ActionController :: UnknownFormat

I am trying to do partial with ajax, but for some reason it returns this error:

ActionController::UnknownFormat in ThingsController#upvoterandom ActionController::UnknownFormat 

I am very confused because I used to do something with a substantially identical format and I never had any problems with it. Does anyone see something wrong with my code? I can make a line with ajax; it is only when I try to do partial to get an error. By the way, I achieved this error by deleting the format.html line and then visiting the upvoterandom_thing path directly in my browser.

view / stuff / show.html.erb

 <div id= "randomajax" > <div id="randajax"> <%= link_to @rand.name, thing_path(@rand) %> <%= link_to image_tag("UpArrowGray.jpg", class: "rand_up_vote"), remote: true, %> <script type="text/javascript"> function reload_script() { $(".rand_up_vote").click(function () { $.get( "<%= upvoterandom_thing_path(:id => @rand.id) %>", function( data ) { }); }); } reload_script(); </script> </div> </div> 

controllers / things_controller.rb I put asterisks around the line highlighted with an error.

 def upvoterandom @thing = Thing.find(params[:id]) #... ***respond_to do |format|*** format.html { redirect_to root_path } format.js end end 

views / things / upvoterandom.js.erb: .html ("test") returns "test", so the problem should be in rendering.

 $('#randomajax').html("<%= j(render(@randajax)) %>"); 

view / stuff / _randajax.html.erb

 TEST 

THESE OTHER PAGES THAT WORK:


view / stuff / show.html.erb

 <%= form_for([@thing, @comment], remote: true) do |f| %> <%= f.text_area :text %> <%= f.submit "Post", id: "postacomment" %> <% end %> 

Controllers / comments _controller.rb

 def create @thing = Thing.find(params[:thing_id]) @comment = @thing.comments.create(comment_params) respond_to do |format| format.html { redirect_to root_path } format.js end end 

view / comments / create.js.erb

 $('#comments_h2').prepend("<%= j(render(@comment)) %>"); 

view / comments / _comment.html.erb

 TEST 
+6
source share
3 answers

Well, I didn’t understand why the syntax I tried did not work, but this syntax really works:

 $('#randomajax').html("<%= render 'randajax' %>"); 
+1
source

I apologize for the long answer. I tried to reproduce your problem in several ways, but I think you can look in the wrong direction. I have included the whole story so that you can see if it all fits, and if she does not expect her, she will lead to an understanding that will help you solve the problem.

ActionController::UnknownFormat happens before rendering view

If Rails presents you with an ActionController::UnknownFormat , it means that your controller is not responding to the requested format. The error is raised by the respond_to method, in the exact line that you selected.

If an error was made during rendering, then it would be bubbled through the line using format.html or format.js . Thus, this error, of course, is not caused by part of the rendering of the view.

Error ActionController::UnknownFormat

The only way to get the exact error message you sent is if I request the AJAX response page directly in the browser. Then you will see the Rails error page, which will also show you in what format the request was executed. You are probably requesting a page with a format other than html or js , in which case the ActionController::UnknownFormat error is ActionController::UnknownFormat , since your controller only responds to the html or js format.

I think the real problem is hiding somewhere else.

Playback from views/things/show.html.erb

I tried to reproduce the error in the views/things/show.html.erb page. When you do this with your source code, I get a syntax error in the following line:

 <%= link_to image_tag("UpArrowGray.jpg", class: "rand_up_vote"), remote: true, %> 

This is due to the comma after remote: true . Because of this, I suppose you did not constantly test the remote link. When testing without remote: true , the reload_script function launches a jQuery AJAX request. You do this with the following line:

 $.get( "<%= upvoterandom_thing_path(:id => @rand.id) %>", function( data ) { }); 

This actually triggers an XHR request in */* format. It basically tells Rails what any format will do. Here's what happens:

  • ThingsController responds with the first format you defined in the respond_to , which is the HTML format. This causes a redirect to your root path.
  • jQuery follows the redirect, again using the */* format.
  • The controller in the root path responds with the first defined format or the default HTML if there is no respond_to .
  • JQuery then loads this answer.

It is not possible that an ActionController::UnknownFormat could be raised from a ThingsController when it is played as follows.

What I think

This is mostly a guessing job, so please correct me if I am wrong:

  • You clicked the link of the deleted link and nothing happened.
  • You added JavaScript to explicitly get the page through AJAX, but still nothing happened.
  • You have visited things/1/upvoterandom.js (or similar) page directly in your browser. In this case, ActionController::InvalidCrossOriginRequest would be raised.
  • You visited things/1/upvoterandom.json (I used JSON, but it could be any format other than HTML or JS), directly with your browser, and you got an ActionController::UnknownFormat .

If so, you need to go back to step 1 and start reproducing the problem in the other direction:

  • Go to the things/1 page (or another page).
  • Open the developer tool / web inspector of your browser.
  • Go to the view that shows the network connection (in Chrome, this is the "Network" tab).
  • Now click on the link and see what happens between your browser and your Rails application. If a lot happens, you can often filter only for XHR, which only shows AJAX requests.
  • If you see status there 500, you need to check the output of the Rails server for an error using stacktrace.

I can only guess what the actual cause of the problem is, but I think there might actually be some error in rendering the view. As you said, the only case when it does not work is partial. I hope this information helps you solve the problem.

+4
source

In views/things/upvoterandom.js.erb you are trying to do @randajax as follows:

 $('#randomajax').html("<%= j(render(@randajax)) %>"); 

But I do not see that @randajax is assigned anywhere, which means that it is nil .

Trying to do nil is the cause of this problem. But confirm this by doing the following:

 $('#randomajax').html("<%= j(render(nil)) %>"); 

If he returns the same error, we have found the culprit.

Either assign @randajax something before rendering it, or just use:

 $('#randomajax').html("<%= j(render(path_to_partial)) %>"); 
+2
source

Source: https://habr.com/ru/post/979760/


All Articles