When trying to render a partial using ajax, I encountered an error that reads as follows:
ActionController::UnknownFormat in ThingsController#upvoterandom
ActionController::UnknownFormat
This error is baffling because I previously achieved a similar task with no issues. Can anyone spot what might be wrong with my code? The error occurs only when attempting to render a partial, not when rendering a string with ajax. The error was triggered by removing the format.html
line and then directly accessing the upvoterandom_thing
path in my browser.
views/things/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 The line with the error is highlighted by asterisks.
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 issue must lie in the rendering.
$('#randomajax').html("<%= j(render(@randajax)) %>");
views/things/_randajax.html.erb
TEST
THIS IS THE OTHER NEAR-IDENTICAL AJAX STRUCTURE THAT WORKS:
views/things/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
views/comments/create.js.erb
$('#comments_h2').prepend("<%= j(render(@comment)) %>");
views/comments/_comment.html.erb
TEST