找不到用于SomeController#create渲染头的模板:no_content

时间:2018-11-06 17:39:39

标签: javascript ruby-on-rails ruby ajax

我正在构建一个rails应用程序,该应用程序在带有远程链接的单击上创建一个like(创建)或likes(销毁)(destroy)

帖子通过且事务已提交,但js响应未通过,我在服务器控制台中收到错误

No template found for ForumThreads::ForumPosts::LikesController#create, head :no_content

视图结构是

forum_threds/show.html.haml
....
  = render @forum_thread.forum_posts
....

呈现线程中每个帖子的视图 比

forum_posts/_forum_post.html.haml
....
  %div{id: "fourm_post_#{forum_post.id}" }
  = render partial: "forum_posts/likes", locals: { post: forum_post }
....

正在渲染

forum_posts/_likes.html.haml
- if user_signed_in? && current_user.likes?(post)
  = link_to "UnLike", forum_post_likes_path(post), method: :delete, 
    remote: true
- else
  = link_to "Like", forum_post_likes_path(post), method: :post, remote: 
    true

我的控制器是

class ForumThreads::ForumPosts::LikesController < ApplicationController
  before_action :authenticate_user!
  before_action :set_forum_post
  before_action :set_forum_thread

  def create
    @forum_post.likes.where(user_id: current_user.id).first_or_create

    respond_to do |format|
      format.js
      format.html { redirect_to @forum_thread}
    end
  end

  def destroy
    @forum_post.likes.where(user_id: current_user.id).destroy_all

    respond_to do |format|
      format.js
      format.html { redirect_to @forum_thread}
    end
  end

  private

  def set_forum_thread
    @forum_thread = ForumThread.find(@forum_post.forum_thread_id)
  end

  def set_forum_post
    @forum_post = ForumPost.find(params[:forum_post_id])
  end
end

使用远程:false可以按预期在页面重新加载时正常工作,但是当我放置远程:true时会收到无模板错误

我的视图文件结构

  • 论坛线程/
    • show.html.haml
    • forum_posts /
      • show.html.haml
      • _likes.html.haml
      • 喜欢/
        • create.js.erb
        • destroy.js.erb

以及create.js.erb和destroy.js.erb

$("#forum_post_<%= forum_post.id %>").html(<%= j render 
  'forum_posts/likes' %>);

但是我知道这个文件甚至都没有被调用

从format.js是否可以直接调用我需要的文件?还是什么?

0 个答案:

没有答案