Ajax on Rails

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

Hey Mick, please provide the relevant view code.

Thanks,

-Conrad

On Apr 1, 5:47 pm, Mick [email protected] 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

Yep - found that out with a bot of work,

cheers

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

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)