Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6383771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:41:30+00:00 2026-05-25T02:41:30+00:00

My setup: Rails 3.0.9, Ruby 1.9.2, jQuery 1.6.2 HTML <textarea id=photo-42-9 class=comment_box>Write a comment…</textarea>

  • 0

My setup: Rails 3.0.9, Ruby 1.9.2, jQuery 1.6.2

HTML

<textarea id="photo-42-9" class="comment_box">Write a comment...</textarea>

jQuery

$('#newsfeed').delegate('.comment_box', 'keydown', function (event){
   if(event.keyCode == 13) {
    event.preventDefault();
    $.post('/comments', { title: ..., description: ... });
  }
});

Rails

comments_controller.rb    
  def create
    @comment = Comment.new

    respond_to do |format|
      format.html # new.html.erb
      format.js
    end
  end

create.js.erb
$("<%= escape_javascript( render 'show_comments') %>").insertBefore(???);

render 'show_comments' returns a <div>stuff</div> that I wish to insert before textarea. I could use the selector on #photo-42-9 but that id is dynamic depending on which textarea element I clicked on. How do I access this object in create.js.erb?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:41:31+00:00Added an answer on May 25, 2026 at 2:41 am

    in js:

    $('#newsfeed').delegate('.comment_box', 'keydown', function (event){
       if(event.keyCode == 13) {
        event.preventDefault();
        var params = { title: ..., description: ... };
        var $this = $(event.target);
        $.post('/comments', params, function(response) {
            $this.before(response);
        });
      }
    });
    

    and in ruby (i guess, I don’t do ruby):

    "<%= escape_javascript( render 'show_comments') %>"
    

    As you see, “this” isn’t passed to rb. Instead, the $.post function waits for the rb response, and pastes it before the text area (the event target). This way is better as all your js code is actually in your js code.

    note: “$(response).insertBefore($this)” would work the same.

    note: if you move the $this assigination out of the “if”, you can easily check if the fresh comment is not empty and different than the ones already existing (event.keyCode == 13 && $this.val() != '' && $this.val() != $this.prev().text()). As well, you can empty the textarea with $this.val('') right after the “$this.before” insertion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My setup: Rails 3.0.9, Ruby 1.9.2, jQuery 1.6.2 I'm making an AJAX call from
My setup: Rails 3.0.9, Ruby 1.9.2 Here are my models class Project has_many :tasks
I'm trying to get Ruby on Rails setup on my new eeeubuntu install and
I've been trying to get setup with Ruby on Rails today, but I think
I have a simple model setup in my Ruby on Rails app. (User {name,
Hey guys I have a ruby on rails app with a before filter setup
My setup: Rails 3.0.9, Ruby 1.9.2 I am using Cancan to authorize a controller
I'm attempting to setup Ruby on Rails on IIS7. I've been reading lots of
My setup: Rails 2.3.10, Ruby 1.8.7 This is a simple question but I want
My setup: Rails 2.3.10, Ruby 1.8.7 I have a rather complicated set of relationships

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.