我想在我的home / index.html.erb页面上显示我的所有帖子。我有一个表单来创建一个新帖子,但是当我点击提交时,AJAX请求没有问题,但响应是空白的
这是我的帖子/ create.js.erb
$('#all-posts').html(" <%= j (render @posts) %> ");
$('#post-form').slideUp(350);
但是在Chrome开发工具中的反应是
$('#all-posts').html(" ");
$('#post-form').slideUp(350);
posts_controller.rb
class PostsController < ApplicationController
before_action :all_posts, only: [:index, :create]
respond_to :html, :js
def new
@post = Post.new
end
def create
@post = Post.create(post_params)
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
@post.update_attributes(post_params)
end
def destroy
@post = Post.find(params[:id])
@post.destroy
end
private
def post_params
params.require(:post).permit(:content)
end
def all_posts
@posts = Post.all
end
end
home_controller.rb
class HomeController < ApplicationController
def index
@posts = Post.all
end
end
这是我想要显示post home / index.html.erb
的地方<div class="row">
<div class="col-md-7 col-md-offset-1" id="all-posts">
<%= render @posts %>
</div>
</div>