首页 > 解决方案 > 找不到 SomeController#create 渲染头的模板:no_content

问题描述

我正在构建一个 Rails 应用程序,该应用程序创建一个喜欢(创建)或不同(销毁),在链接上单击远程:真正的 ajax 请求

帖子通过并提交了事务,但 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

使用 remote: false 这在页面重新加载时按预期工作,但是当我放 remote: true 时,我收到没有模板错误

我的视图文件结构

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

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

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

有没有一种方法可以从 format.js 中直接调用我需要的文件?或者其他的东西?

标签: javascriptruby-on-railsrubyajax

解决方案


推荐阅读