所以我有一个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
的操作是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'
答案 0 :(得分:0)
如果在Rails视图中,它是
<div class="card" id="card-<%= @card_number %>">
然后对于Javascript,你应该使用:
$("#card-<%= @card_id %>")