首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过StimulusJS远程渲染/获取模板?

如何通过StimulusJS远程渲染/获取模板?
EN

Stack Overflow用户
提问于 2021-01-03 09:25:26
回答 1查看 71关注 0票数 0

因此,下面是我想要在特定事件发生后添加到HTML中的代码:

代码语言:javascript
复制
<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>

到目前为止,我能找到的唯一方法是:

代码语言:javascript
复制
  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上,我找不到与此相关的问题。

EN

回答 1

Stack Overflow用户

发布于 2021-01-03 11:29:24

如果您希望连续呈现评论表单。您可以像这样使用循环:

代码语言:javascript
复制
 <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>

而且,如果你想添加一些特定的东西。你可以在有条件的情况下从控制器添加部分。

代码语言:javascript
复制
def create
  if comment.create 
    render partial: "posts/comment"
  end
end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65545822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档