gpt4 book ai didi

javascript - Rails 5.1.2 Ajax 远程 : true not working

转载 作者:行者123 更新时间:2023-12-01 00:21:54 25 4
gpt4 key购买 nike

我想实现 ajax 来创建帖子,无需给页面充电,也不去观看演出(我按照教程进行操作),但不起作用。

在_form.html.haml中,您可以在表单助手中看到remote:true

= form_for @post,remote: true do |f| 
- if @post.errors.any?
#error_explanation
%h2= "#{pluralize(@post.errors.count, "error")} prohibited this post
from being saved:"
%ul
- @post.errors.full_messages.each do |message|
%li= message

.mdl-textfield.mdl-js-textfield.full-width
= f.text_area :body, class:"mdl-textfield__input"
= f.label :body, "Comparte con la Comunidad", class:"mdl-
textfield__label"
.actions.text-right
= f.submit 'Publicar', class:"mdl-button mdl-js-button mdl-button--
raised mdl-button--colored"

但是,当创建帖子时,应用程序会重定向到最近以 html 格式创建的帖子的显示,它应该保留在表单中。

我不知道 Rails 5.1.2 版本是否有问题,但在 Rails 4 中,只需键入remote: true,应用程序在提交操作后仍保留在表单中。

后置 Controller :

class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]

# GET /posts
# GET /posts.json
def index
@posts = Post.all
end

# GET /posts/1
# GET /posts/1.json
def show
end

# GET /posts/new
def new
@post = Post.new
end

# GET /posts/1/edit
def edit
end

# POST /posts
# POST /posts.json
def create
@post = current_user.posts.new(post_params)

respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end

# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:body)
end
end

我还有一个 show.js.erb 文件来渲染一个 View ,以便在创建帖子后查看帖子的正文。

show.js.erb

$('#posts').append("<%= render j @post %>")

_post.haml

%article
=post.body

最佳答案

完成!在 post Controller 中缺少响应 JS 格式,并且 show.js.erb 的渲染方法语法错误

1)后置 Controller :添加format.js

def create
@post = current_user.posts.new(post_params)

respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
format.js { render :show }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
format.js { }
end
end

结束

2) show.js.erb:渲染前应该出现的 J

$("posts").append(<%= j render @post %>)

关于javascript - Rails 5.1.2 Ajax 远程 : true not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45244394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com