Forum: Ruby on Rails Insert text into text area field with RJS

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
58a2238def3602fc1fb2e94a28cd6063?d=identicon&s=25 Ingo Paulsen (dite)
on 2007-03-25 20:10
Hi,

I'd like to insert/append some default text into a text area after
clicking on an image button 'test.png' in edit.rhtml:

<%= link_to image_tag("test.png"), url => { :action => :new_text } %>

Action:

# article_body is the textarea's id.

def new_text
  render :update do |page|
    page.insert_html :bottom, :article_body, "text\n"
  end
end

Not surprisingly insert_html doesn't work properly in this innerHTML
context. But how it is possible to append text to existing one?

page[:article_body][:value] = "text"

e.g. overwrites the old content completely.

Do I need to use request.raw_post or reques.query_string in some way?


Best regards
Ingo
8bc543795b502900b5333aea73ad5533?d=identicon&s=25 Eden Li (edenli)
on 2007-03-25 21:31
(Received via mailing list)
When in doubt, drop to javascript:

page << "$('article_body').value += 'blah'"

On Mar 26, 2:10 am, Ingo Paulsen <rails-mailing-l...@andreas-s.net>
58a2238def3602fc1fb2e94a28cd6063?d=identicon&s=25 Ingo Paulsen (dite)
on 2007-03-25 23:23
Thanks for this Eden,

just wondering:

page << "$('article_body').value += '\n\\blah\n'"

with some extra newlines and a backslash works in a javascript function:

function insertText(obj,myText) {
  //obj.value += myText;
  $('article_body').value += '\n\\blah\n'
}

but not within a RJS environment (only tabs for example):

render :update do |page|
  page << "$('article_body').value += '\n\\blah\n'"
end

Any ideas?


Regards
Ingo

> When in doubt, drop to javascript:
>
> page << "$('article_body').value += 'blah'"
>
> On Mar 26, 2:10 am, Ingo Paulsen <rails-mailing-l...@andreas-s.net>
8bc543795b502900b5333aea73ad5533?d=identicon&s=25 Eden Li (edenli)
on 2007-03-26 04:17
(Received via mailing list)
Where is your RJS being loaded up?

Your escaping is a off with the newlines, so that could be the issue.
When you use #<<, you'll be dumping a string directly to javascript,
and ruby's quoting is getting in the way, so "$('article_body').value
+= '\n\\blah\n'" would be sent to your browser as:

"$('article_body').value += '
\blah
'

Instead you should escape the newlines.  Also, you'll need to double
escape the \\ before the b, because '\b' will get interpreted in
javascript as a backspace character: "$('article_body').value += '\\n\\
\\blah\\n'". Or you can use ruby single quotes and heredocs to make
things look nicer although you won't be able to insert variables into
the string block.

page << <<-'end'
  $('article_body').value += '\n\\blah\n';
end

On Mar 26, 5:23 am, Ingo Paulsen <rails-mailing-l...@andreas-s.net>
58a2238def3602fc1fb2e94a28cd6063?d=identicon&s=25 Ingo Paulsen (dite)
on 2007-03-26 09:48
Thanks again Eden,

this was helpful for me. I didn't get the double backslash (escape)
point yesterday.


Regards
Ingo

> Where is your RJS being loaded up?
>
> Your escaping is a off with the newlines, so that could be the issue.
> When you use #<<, you'll be dumping a string directly to javascript,
> and ruby's quoting is getting in the way, so "$('article_body').value
> += '\n\\blah\n'" would be sent to your browser as:
>
> "$('article_body').value += '
> \blah
> '
>
> Instead you should escape the newlines.  Also, you'll need to double
> escape the \\ before the b, because '\b' will get interpreted in
> javascript as a backspace character: "$('article_body').value += '\\n\\
> \\blah\\n'". Or you can use ruby single quotes and heredocs to make
> things look nicer although you won't be able to insert variables into
> the string block.
>
> page << <<-'end'
>   $('article_body').value += '\n\\blah\n';
> end
>
> On Mar 26, 5:23 am, Ingo Paulsen <rails-mailing-l...@andreas-s.net>
C95664a5c59ccdfb0da2f5910675c62c?d=identicon&s=25 Sudhi Kulkarni (sudhi)
on 2009-03-19 11:46
Ingo Paulsen wrote:
> Thanks again Eden,
>
> this was helpful for me. I didn't get the double backslash (escape)
> point yesterday.
>
>
> Regards
> Ingo
>
>> Where is your RJS being loaded up?
>>
>> Your escaping is a off with the newlines, so that could be the issue.
>> When you use #<<, you'll be dumping a string directly to javascript,
>> and ruby's quoting is getting in the way, so "$('article_body').value
>> += '\n\\blah\n'" would be sent to your browser as:
>>
>> "$('article_body').value += '
>> \blah
>> '
>>
>> Instead you should escape the newlines.  Also, you'll need to double
>> escape the \\ before the b, because '\b' will get interpreted in
>> javascript as a backspace character: "$('article_body').value += '\\n\\
>> \\blah\\n'". Or you can use ruby single quotes and heredocs to make
>> things look nicer although you won't be able to insert variables into
>> the string block.
>>
>> page << <<-'end'
>>   $('article_body').value += '\n\\blah\n';
>> end
>>
>> On Mar 26, 5:23 am, Ingo Paulsen <rails-mailing-l...@andreas-s.net>

Hi,

This helps for me but I have 1 doubt

def add_text_link(name)
    link_to_function name do |page|
begin
        page << <<-'end'
  $('text_area').value += "\n Hello world"  ;
end
end

I added above code in the RJS, this works but if I need to use the name
parameter that is passed in how do I do it?

I tried #{name} but no luck...

Thanks,
Sudhindra
This topic is locked and can not be replied to.