[英]How do I select a dynamic ID represented by a ruby instance variable?
因此,我有一個div
其ID由我視圖中的實例變量填充:
<div class="card" id="card-<%= @card_number %>">
在我的create.js.erb
,如何選擇該div?
我嘗試了這個:
$('#<%= @card_id %> .card-comments').prepend("<%= j (render partial: 'nodes/comment', locals: { comment: @comment}) %>");
但它不起作用,並在選擇器中為ID輸出空白值。 如何選擇正確的ID?
我還嘗試了$('#<%= j @card_id %> .card-comments').prepend...
但這也不起作用。
編輯1
這是在視圖中生成該動態ID后綴的代碼:
<% @card_number = 0 %>
<% @nodes.each do |node| %>
<% @card_number += 1 %>
<div class="col s12 m6 l4 card-container">
<div class="card" id="card-<%= @card_number %>">
<!-- Card Content -->
這樣會生成如下HTML:
<div class="card" id="card-2">
編輯2
我的控制器中調用create.js.erb
是create.js.erb
CommentController#Create
。 看起來是這樣的:
def create
@node = Node.find(params[:node_id])
@comment = current_user.comments.new(comment_params)
@comment.node = @node
@card_id = params[:card_id]
respond_to do |format|
if @comment.save and @node.save
format.html
format.js
else
format.html # { render action: 'new' }
format.js
end
end
end
編輯3
這是生成表單的代碼:
<div class="card-input">
<%= simple_form_for([node, Comment.new], html: { id: "new_comment_card-#{@card_number}"}, remote: true) do |f| %>
<%= f.error_notification %>
<%= f.input_field :message, as: :text, id: "card-input-field-#{@card_number}", class: "input-field", placeholder: "Share your thoughts", cols: "30", rows: "10" %>
<%= f.button :submit, name: "card_id", value: "card-#{@card_number}", class: "input-submit" %>
<% end %>
</div>
編輯4
提交表單后,服務器日志如下所示:
Started POST "/nodes/86/comments" for 127.0.0.1 at 2015-07-13 23:22:26 -0500
Processing by CommentsController#create as JS
Parameters: {"utf8"=>"✓", "comment"=>{"message"=>"Form Video Comment 5."}, "card_id"=>"card-2", "node_id"=>"86"}
User Load (5.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 57 ORDER BY "users"."id" ASC LIMIT 1
FamilyTree Load (7.8ms) SELECT "family_trees".* FROM "family_trees" WHERE "family_trees"."user_id" = $1 LIMIT 1 [["user_id", 57]]
Role Load (4.5ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 57]]
Node Load (2.3ms) SELECT "nodes".* FROM "nodes" WHERE "nodes"."id" = $1 LIMIT 1 [["id", 86]]
(1.9ms) BEGIN
SQL (5.1ms) INSERT INTO "comments" ("created_at", "message", "node_id", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["created_at", "2015-07-14 04:22:26.760577"], ["message", "Form Video Comment 5."], ["node_id", 86], ["updated_at", "2015-07-14 04:22:26.760577"], ["user_id", 57]]
SQL (6.2ms) UPDATE "nodes" SET "comments_count" = COALESCE("comments_count", 0) + 1 WHERE "nodes"."id" = 86
(3.3ms) COMMIT
(7.7ms) BEGIN
(1.8ms) COMMIT
Rendered nodes/_comment.html.erb (1.2ms)
Rendered comments/create.js.erb (11.6ms)
Completed 200 OK in 351ms (Views: 29.3ms | ActiveRecord: 46.2ms)
注意params[:card_id] = 'card-2'
如果在Rails視圖中,則為
<div class="card" id="card-<%= @card_number %>">
然后,對於Javascript,您應該使用:
$("#card-<%= @card_id %>")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.