Javascript Index.js.erb文件在rails 5.1中不起作用

Javascript Index.js.erb文件在rails 5.1中不起作用,javascript,ruby-on-rails,rubygems,ruby-on-rails-5,Javascript,Ruby On Rails,Rubygems,Ruby On Rails 5,我正在学习devfactor.com的教程。它被称为“Let's Build:Instagram with Rails”。作者在教程中使用了AJAX和js文件。当添加一个名为“Index.js.erb”的文件时,它显示了我的服务器中的许多错误。 错误: application.js: // This is a manifest file that'll be compiled into application.js, which will include all the files // lis

我正在学习devfactor.com的教程。它被称为“Let's Build:Instagram with Rails”。作者在教程中使用了AJAX和js文件。当添加一个名为“Index.js.erb”的文件时,它显示了我的服务器中的许多错误。 错误:

application.js:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require rails-ujs
//= require turbolinks
//= require_tree 
就这样。错误消息在上面。如果你有任何问题,请发表评论。非常感谢。 这是我的帖子管理员:

class PostsController < ApplicationController
    before_action :set_post, only: [:show, :edit, :update, :destroy]
    before_action :authenticate_user!
    before_action :owned_post, only: [:edit, :update, :destroy]
    def index
      @posts = Post.all.order('created_at DESC').page params[:page]
      respond_to do |format|
        format.js
        format.html
      end
    end

    def show
    end

    def new
      @post = current_user.posts.build
    end

    def create
      @post = current_user.posts.build(post_params)
      if @post.save
        redirect_to posts_path
        flash[:success] = "Post Created!"
      else
        flash.now[:success] = "Your new post couldn't be created!  Please check the form."
        render :new
      end
    end

    def edit
    end

    def update
      if @post.update(post_params)
        redirect_to posts_path
      else
        flash.now[:alert] = "Update failed.  Please check the form."
        render :edit
      end
    end

    def destroy
      @post.destroy
      redirect_to root_path
      flash[:success] = "Destroyed ;)"
    end

    private

    def owned_post
      unless current_user == @post.user
        flash[:success]= "That post doesn't belong to you :("
        redirect_to root_path
      end
    end

    def post_params
      params.require(:post).permit(:image, :caption)
    end

    def set_post
      @post = Post.find(params[:id])
    end
  end
class PostsController
您需要渲染部分,例如

$('#posts').append('<%= escape_javascript(render @posts) %>');
$('#posts')。附加('');

您需要渲染部分,例如

$('#posts').append('<%= escape_javascript(render @posts) %>');
$('#posts')。附加('');

您的
index.html.erb
文件缺少
#posts
选择器,该选择器用于
$('#posts')。请在
index.js.erb
中追加('')
命令

<div id="posts">
  <%= render 'posts' %>
</div>
<div class="text-center" id="paginator">
  <%= link_to_next_page @posts, 'LOAD MORE', remote: true, id: 'load_more' %>
</div> 


正如错误所提示的,您还需要一个
posts/_post.html.erb
partial,这是JS partial试图呈现的。

您的
index.html.erb
文件缺少
#posts
选择器,用于
$('#posts')。在
index.JS erb
中添加(''/code>命令

<div id="posts">
  <%= render 'posts' %>
</div>
<div class="text-center" id="paginator">
  <%= link_to_next_page @posts, 'LOAD MORE', remote: true, id: 'load_more' %>
</div> 



正如错误所提示的,您还需要一个
posts/\u post.html.erb
partial,这是JS partial试图呈现的内容。

将您的控制器添加到问题中。在这里,我添加了它!您的视图/posts/\u posts是部分的吗?是的,我应该包括它吗?将您的控制器添加到问题中。在这里,我添加了它!您有视图/posts/\u posts吗部分?是的,我应该包括它吗?我做了,但我想让它做的动作不起作用?你也需要使用单引号。我编辑了我的回答,但当我点击按钮时,什么都没有发生?我应该重新启动服务器吗?它呈现文件但什么都没有。为什么?我做了,但我想让它做的动作不起作用?你需要使用单引号o、 我编辑了我的回答。当我点击按钮时,什么也没发生。我应该重新启动服务器吗?它会呈现文件,但什么也不做。为什么?你能写出我需要在我的文件中写入的内容吗?那么,错误停止了,按钮什么都不做。为什么?好的,我添加了一个例子。你能写出我需要在我的文件中写入的内容吗lso,错误停止了,按钮没有任何作用。为什么?好的,我添加了一个例子。
class PostsController < ApplicationController
    before_action :set_post, only: [:show, :edit, :update, :destroy]
    before_action :authenticate_user!
    before_action :owned_post, only: [:edit, :update, :destroy]
    def index
      @posts = Post.all.order('created_at DESC').page params[:page]
      respond_to do |format|
        format.js
        format.html
      end
    end

    def show
    end

    def new
      @post = current_user.posts.build
    end

    def create
      @post = current_user.posts.build(post_params)
      if @post.save
        redirect_to posts_path
        flash[:success] = "Post Created!"
      else
        flash.now[:success] = "Your new post couldn't be created!  Please check the form."
        render :new
      end
    end

    def edit
    end

    def update
      if @post.update(post_params)
        redirect_to posts_path
      else
        flash.now[:alert] = "Update failed.  Please check the form."
        render :edit
      end
    end

    def destroy
      @post.destroy
      redirect_to root_path
      flash[:success] = "Destroyed ;)"
    end

    private

    def owned_post
      unless current_user == @post.user
        flash[:success]= "That post doesn't belong to you :("
        redirect_to root_path
      end
    end

    def post_params
      params.require(:post).permit(:image, :caption)
    end

    def set_post
      @post = Post.find(params[:id])
    end
  end
$('#posts').append('<%= escape_javascript(render @posts) %>');
<div id="posts">
  <%= render 'posts' %>
</div>
<div class="text-center" id="paginator">
  <%= link_to_next_page @posts, 'LOAD MORE', remote: true, id: 'load_more' %>
</div>