因此,下面是我想要在特定事件发生后添加到HTML中的代码:
<div class='comment-form' data-controller='comment'>
<form action='' data-action='comment#createComment'>
<div class='comment-form__title'>Add your reply:</div>
<textarea class='comment-form__textarea' placeholder='Type in your reply' data-comment-target='commentText'></textarea>
<input type="submit" value="Submit" class='btn-secondary' >
</form>
</div>到目前为止,我能找到的唯一方法是:
post.insertAdjacentHTML(`<div class='comment-form' data-controller='comment'>
<form action='' data-action='comment#createComment'><div class='comment-form__title'>Add your reply:</div><textarea class='comment-form__textarea' placeholder='Type in your reply' data-comment-target='commentText'></textarea><input type="submit" value="Submit" class='btn-secondary'></form></div>`)我正在寻找一种通过服务器代码(读作: Rails)呈现该模板的方法,而不是在JS中对其进行硬编码。
附言:
在写这篇文章的时候,StimulusJS的讨论已经停止了,我可以找到一个类似于我在这里问的问题的链接:https://discourse.stimulusjs.org/t/fetching-a-partial-on-click/1297,而在Stackoverflow上,我找不到与此相关的问题。
发布于 2021-01-03 11:29:24
如果您希望连续呈现评论表单。您可以像这样使用循环:
<div class='post-comments-section'>
<% post.comments.each do |comment| %>
<div class="post-comments">
<p>
<b><%= comment.user.name %>:</b> <%= comment.content %>
</p>
<span> <%= comment.created_at.strftime("%Y/%m/%d") %> </span>
</div>
<% end %>
<%= form_for(post.comments.new, url: post_comments_path(post)) do |form| %>
<%= form.text_field :content, id: :comment_content, class: 'form-control', placeholder: 'Add new Comment' %>
<%= form.submit 'Comment', class: 'btn btn-secondary' %>
<% end %>
</div>而且,如果你想添加一些特定的东西。你可以在有条件的情况下从控制器添加部分。
def create
if comment.create
render partial: "posts/comment"
end
endhttps://stackoverflow.com/questions/65545822
复制相似问题