簡體   English   中英

將Div ID從Rails控制器動態傳遞到js.erb文件

[英]Passing Div ID Dynamically from Rails controller to js.erb file

我正在創建一個具有任務的應用程序,用戶可以喜歡並對任務進行評論。 我在索引頁面本身帶來了like和comments功能。

索引頁面為每個任務呈現部分注釋:

任務/ _comments.html.erb

<div class="comments-div">
<% if comments.present? %>
<% comments.each do |comment| %>
    <div class="comment-div">
        <div><img src="/assets/user.png" class="comment-img">
        <p class='comment-desc'><%= comment.description %></p></div>
        <p class="height-10">User: <%= comment.user.name %></span>
            <span class="right">Commented On: <%= comment.created_at.strftime("%d %b %y") %></span>     
        </p>
    </div>
<% end %>
<% end %>
</div>
<div class="comments-form">
            <%= form_for :comment, url: "comments/add_comments", remote: true, html: { id: 'comment-form' } do |form| %>

              <%= form.hidden_field :task_id, value: task_id %>
              <div class="search-fields">
                <%= form.text_area :description %>
              </div>

              <div>
                <%= form.submit 'Comment', id: 'ad-comment-btn', remote: true, 'data-task-id' => task_id, click: 'form_action(this);' %>
              </div>
            <% end  %>

</div>

這個js在添加評論表單提交時執行。

的application.js

    $('.comments-form form').submit(function(e){
    e.preventDefault();
    var taskId = this.elements[1].value;
    var description = this.elements[2].value;
    var comment_params = JSON.stringify({task_id: taskId, description: description});
    $.post("/comments/add_comments?comment=" + comment_params);
    return false;
})

comments_controller:

def add_comments
    comment_params = JSON.parse(params[:comment])
    comment_params.merge!({user_id: current_user.id})
    @comment = Comment.create(comment_params)
    @task_container_id = "task-comments-container_#{comment_params['task_id']}"
    @comments = Comment.where(task_id: comment_params["task_id"]).to_a
    @task_id = comment_params["task_id"]

    if @comment.save
        flash[:success] = "Comment created Successfully"
    else
        flash[:error] = "Something went wrong. Try again"
    end

    respond_to do |format|
  format.js       
end
end

我從控制器傳遞@task_container_id並嘗試在js.erb文件中使用它。

add_comments.js.erb

$('#"<%= @task_container_id %>"').html("<%= escape_javascript(render partial: 'tasks/comments', locals: { comments: @comments, task_id: @task_id } ) %>"); 

我想替換控制器傳遞的$('#“<%= @task_container_id%>”')中的div id。 但div沒有像預期的那樣令人耳目一新 如果我在js.erb文件中對div id進行硬編碼,則刷新正常。

我如何解決它?

aplication.jselements array is zero based ,因此在獲取taskId和description時分別將值更改為0和1

var taskId = this.elements[0].value;
var description = this.elements[1].value;

- - - - - - - - - - - - 更新 - - - - - - - - - - - - -

我猜我發現了這個錯誤

在你的控制器中

@task_id = comment_params["task_id"]更改為@task_id = comment_params[:task_id]

在此輸入圖像描述

你可以試試下面的,

escape_javascript別名為j因此您可以使用它。 我假設您已經獲得了具有此動態ID的元素,並且已經使用動態ID在該div中呈現了'tasks/comments'部分。

add_comments.js.erb

$("#'<%= @task_container_id %>'").html("<%= j render(partial: 'tasks/comments', locals: { comments: @comments, task_id: @task_id } ) %>"); 

祝好運!

這是我犯過的一個非常小的錯誤。

add_comments.js.erb

$("#<%= @task_container_id %>").html("<%= escape_javascript(render partial: 'tasks/comments', locals: { comments: @comments, task_id: @task_id } ) %>"); 

不得不刪除額外的單引號(“#'<%= @task_container_id%>'”)到簡單$(“#<%= @task_container_id%>”)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM