# Ajax on Rails

**URL:** <https://www.ruby-forum.com/t/ajax-on-rails/90815>\
**Category:** Rails\
**Created:** [April 2, 2007, 12:47am UTC](https://www.ruby-forum.com/t/ajax-on-rails/90815 "2007-04-02T00:47:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [April 2, 2007, 12:47am UTC](https://www.ruby-forum.com/t/ajax-on-rails/90815/1 "2007-04-02T00:47:22Z")

</div>

Hi guys, I’m starting to meddle a bit with the inbuilt Ajax  
functionality that Rails offers, and would like a little help of  
possible!

I have following Ajax link:

\<%= link\_to\_remote “read”, :update =\> ‘wrapper’, :url =\> {:action =\>  
‘read\_message’, :id =\> message.id} %\>

which calls this action:

def read\_message  
render(:layout =\> false)  
@the\_message = Message.find(params[:id])  
end

but when I try to use @the\_message in read\_message.rhtml it throws  
errors (and if I dump the contents within that view’s RHTML it is  
empty).

Is there a solution to this?

Cheers,

Mick

---

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [April 2, 2007, 1:04am UTC](https://www.ruby-forum.com/t/ajax-on-rails/90815/2 "2007-04-02T01:04:43Z")

</div>

Hey Mick, please provide the relevant view code.

Thanks,

-Conrad

---

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [April 2, 2007, 1:06am UTC](https://www.ruby-forum.com/t/ajax-on-rails/90815/3 "2007-04-02T01:06:43Z")

</div>

On Apr 1, 5:47 pm, Mick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def read\_message  
> render(:layout =\> false)  
> @the\_message = Message.find(params[:id])  
> end
> 
> but when I try to use @the\_message in read\_message.rhtml it throws  
> errors (and if I dump the contents within that view’s RHTML it is  
> empty).

Remember, that render call you have will force rails to render the  
template at that moment.

Just try putting the render call as the last statement in your method,  
_after_ you set your variables.

Jeff

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [April 2, 2007, 1:09am UTC](https://www.ruby-forum.com/t/ajax-on-rails/90815/4 "2007-04-02T01:09:19Z")

</div>

Yep - found that out with a bot of work,

cheers

---

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [April 2, 2007, 1:08am UTC](https://www.ruby-forum.com/t/ajax-on-rails/90815/5 "2007-04-02T01:08:00Z")

</div>

Hi Conrad! Thanks for the reply so soon, I just this second pinpointed  
the problem. I placed render(:layout =\> false) on the last line of the  
“read\_message” action in the controller and now I can access the data in  
@the\_message,

Cheers,

Mick

---

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [April 4, 2007, 9:17am UTC](https://www.ruby-forum.com/t/ajax-on-rails/90815/6 "2007-04-04T09:17:54Z")

</div>

Also sometimes is better to use rjs file instead of rendering rhtml,

Like if you have loading indicator and you need update two or moder  
DOM’s elements,  
Sample:

in form\_rhtml:  
BlaBla…

Loading..

2

comments here
\<%= form\_remote\_tag :url =\> {:controller =\> 'article', :action =\> 'addComment', :id =\> article.id }, :loading =\> "Element.show('loading'), :complete =\> "Element.hide('loading') } %\> ... form goes here with end form tag,,....

Now in action you are saving posted comments, …  
in addComment.rjs:  
page.replace\_html ‘total\_comments’, @comments.size  
page.replace\_html ‘comments’, :partial =\> “comments”

Thats all,  
(mayb i misspelled something, but that’s a way)
