javascript - How do I select a dynamic ID represented by a ruby instance variable? -


so have div id populated instance variable in view:

<div class="card" id="card-<%= @card_number %>"> 

in create.js.erb, how select div?

i tried this:

$('#<%= @card_id %> .card-comments').prepend("<%= j (render partial: 'nodes/comment', locals: { comment: @comment}) %>"); 

but doesn't work , outputs blank value id in selector. how select correct id?

i tried $('#<%= j @card_id %> .card-comments').prepend... doesn't work either.

edit 1

this code generates dynamic id suffix in view:

    <% @card_number = 0 %>       <% @nodes.each |node| %>         <% @card_number += 1 %>         <div class="col s12 m6 l4 card-container">           <div class="card" id="card-<%= @card_number %>">             <!-- card content --> 

that generates html this:

<div class="card" id="card-2"> 

edit 2

the action in controller calls create.js.erb commentcontroller#create. looks like:

  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 |format|       if @comment.save , @node.save         format.html         format.js       else         format.html # { render action: 'new' }         format.js       end     end   end 

edit 3

this code generates form:

<div class="card-input">   <%= simple_form_for([node, comment.new], html: { id: "new_comment_card-#{@card_number}"}, remote: true) |f| %>    <%= f.error_notification %>    <%= f.input_field :message, as: :text, id: "card-input-field-#{@card_number}", class: "input-field", placeholder: "share thoughts", cols: "30", rows: "10" %>     <%= f.button :submit, name: "card_id", value: "card-#{@card_number}", class: "input-submit" %>   <% end %> </div> 

edit 4

when form submitted, server log looks like:

started post "/nodes/86/comments" 127.0.0.1 @ 2015-07-13 23:22:26 -0500 processing commentscontroller#create js   parameters: {"utf8"=>"✓", "comment"=>{"message"=>"form video comment 5."}, "card_id"=>"card-2", "node_id"=>"86"}   user load (5.7ms)  select  "users".* "users"  "users"."id" = 57  order "users"."id" asc limit 1   familytree load (7.8ms)  select  "family_trees".* "family_trees"  "family_trees"."user_id" = $1 limit 1  [["user_id", 57]]   role load (4.5ms)  select "roles".* "roles" inner join "users_roles" on "roles"."id" = "users_roles"."role_id" "users_roles"."user_id" = $1 , (((roles.name = 'admin') , (roles.resource_type null) , (roles.resource_id null)))  [["user_id", 57]]   node load (2.3ms)  select  "nodes".* "nodes"  "nodes"."id" = $1 limit 1  [["id", 86]]    (1.9ms)  begin   sql (5.1ms)  insert "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 "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) 

notice params[:card_id] = 'card-2'

if in rails view, is

<div class="card" id="card-<%= @card_number %>"> 

then javascript, should use:

$("#card-<%= @card_id %>") 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -