Ruby on rails Rails AJAX未呈现部分
我有一个表单可以在sourcescreate上创建一个新的源代码,在这个表单上有一个模式可以创建一个新的作者。用户可以创建要分配给源的新作者,也可以从清单中选择现有作者 理想情况下,如果他们确实需要创建一个新作者,authorscreate表单将创建该作者,并且新作者将自动出现在可与源代码关联的作者清单上 以下是sourcescreate上“作者选择”复选框的代码: 这是创建作者的模式:Ruby on rails Rails AJAX未呈现部分,ruby-on-rails,ajax,Ruby On Rails,Ajax,我有一个表单可以在sourcescreate上创建一个新的源代码,在这个表单上有一个模式可以创建一个新的作者。用户可以创建要分配给源的新作者,也可以从清单中选择现有作者 理想情况下,如果他们确实需要创建一个新作者,authorscreate表单将创建该作者,并且新作者将自动出现在可与源代码关联的作者清单上 以下是sourcescreate上“作者选择”复选框的代码: 这是创建作者的模式: <!-- New Author Modal --> <div class="modal f
<!-- New Author Modal -->
<div class="modal fade" id="newAuthorModal" tabindex="-1" role="dialog" aria-labelledby="newAuthorModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="newAuthorModalLabel">Add a New Author</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<%= render partial: "authors/js_form", locals: {author: @new_author} %>
</div>
</div>
</div>
</div>
下面是authors/create.js:
但是,当您点击submit时,模式不会消失,并且作者不会被添加到复选框列表中,基本上部分不会刷新。当您手动单击“刷新”时,作者会完美地出现
有人知道如何正确地将其应用到AJAX吗?所需的行为是,当您点击submit时,模式消失,检查表部分刷新,因此作者出现
更新
根据评论建议,我将其添加到create.js中,这成功地使模式消失,尽管部分模式没有刷新:
$('#newAuthorModal').modal('hide');
然后,我发现需要将create.js更改为create.js.erb。这以某种方式阻止了模式消失,但产生了一个服务器错误,说明:
NameError - undefined local variable or method `f' for #<#<Class:0x00007f9e1e0c0ad0>:0x00007f9e18101d38>:
app/views/authors/create.js.erb:2:in `_app_views_authors_create_js_erb__3387754959944580247_70158492637620'
第二次更新
在查阅收集复选框的文档后,f。在收集之前,不需要复选框。我删除了它,并从我的create.js.erb中删除了,locals:{f:f}。模态现在正确地解除,错误也消失了,但我仍然必须点击“刷新”按钮才能显示名称。在sourcescreate中出现了一个输入错误:
<div id="#authorsChecklist">
现在它工作了 $'modal'.modal'hide';你能把它添加到create.jsI中吗?我看不出你在任何地方都有一个表单。authors/create.js中的局部变量f来自哪里?为什么这不会引起命名错误?是像你忘记保存文件或文件路径/名称错误这样简单的事情吗?@7urkm3n我添加了$'newauthormodel'.model'hide';现在它成功地消除了模态。非常感谢。现在我们知道,至少它正在访问create.js。但是,我仍然需要点击“刷新”按钮才能让新作者出现在列表中。@Liz看起来您的结构不好,我也不理解这里的smth。当您从create.js.erb渲染部分时,您并没有在初始表单渲染的上下文中执行此操作。。。也就是说,f不存在。。。。您将需要考虑使用CHECKETBOXYTAG来代替。
$("#authorsChecklist").html("<%= escape_javascript(render partial: 'authors/checklist', locals: { f: f } ) %>");
Started POST "/authors" for ::1 at 2020-01-31 21:23:26 -0800
Processing by AuthorsController#create as JS
Parameters: {"utf8"=>"✓", "author"=>{"first_name"=>"Liz", "middle_initials"=>"", "last_name"=>"Bayardelle", "notes"=>""}, "commit"=>"Save Author Info"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:5
(0.1ms) BEGIN
↳ app/controllers/authors_controller.rb:32
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/controllers/authors_controller.rb:32
Author Create (0.8ms) INSERT INTO "authors" ("first_name", "middle_initials", "last_name", "notes", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["first_name", "Liz"], ["middle_initials", ""], ["last_name", "Bayardelle"], ["notes", ""], ["user_id", 1], ["created_at", "2020-02-01 05:23:26.279449"], ["updated_at", "2020-02-01 05:23:26.279449"]]
↳ app/controllers/authors_controller.rb:32
(0.4ms) COMMIT
↳ app/controllers/authors_controller.rb:32
Rendering authors/create.js
Rendered authors/create.js (0.4ms)
Completed 200 OK in 214ms (Views: 27.8ms | ActiveRecord: 42.7ms)
$('#newAuthorModal').modal('hide');
NameError - undefined local variable or method `f' for #<#<Class:0x00007f9e1e0c0ad0>:0x00007f9e18101d38>:
app/views/authors/create.js.erb:2:in `_app_views_authors_create_js_erb__3387754959944580247_70158492637620'
<div id="#authorsChecklist">
<div id="authorsChecklist">
format.js do
@user_authors = Author.where(user_id: current_user.id)
end