j(render(@partial))返回错误:ActionController :: UnknownFormat

6 javascript ruby ajax ruby-on-rails ruby-on-rails-4

我试图用ajax渲染部分,但由于某种原因它返回此错误:

ActionController::UnknownFormat in ThingsController#upvoterandom
ActionController::UnknownFormat
Run Code Online (Sandbox Code Playgroud)

我很困惑,因为我之前完成了一个基本相同格式的东西,我从来没有遇到任何问题.有人看到我的代码有什么问题吗?我可以用ajax渲染一个字符串; 只有当我尝试渲染部分时才会出现错误.顺便说一句,我通过删除format.html行然后upvoterandom_thing直接在浏览器中访问路径来实现此错误.

意见/事/ 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>
Run Code Online (Sandbox Code Playgroud)

controllers/things_controller.rb我在用错误突出显示的行周围放了星号.

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

views/things/upvoterandom.js.erb: .html("test")返回"test",因此问题必须在渲染中.

$('#randomajax').html("<%= j(render(@randajax)) %>");
Run Code Online (Sandbox Code Playgroud)

意见/事/ _randajax.html.erb

TEST
Run Code Online (Sandbox Code Playgroud)

这是其他近乎相同的AJAX结构:


意见/事/ show.html.erb

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

控制器/ 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
Run Code Online (Sandbox Code Playgroud)

观点/评论/ create.js.erb

$('#comments_h2').prepend("<%= j(render(@comment)) %>");
Run Code Online (Sandbox Code Playgroud)

观点/评论/ _comment.html.erb

TEST
Run Code Online (Sandbox Code Playgroud)

小智 1

好吧,我还没有弄清楚为什么我尝试的语法不起作用,但这个语法确实有效:

$('#randomajax').html("<%= render 'randajax' %>");
Run Code Online (Sandbox Code Playgroud)