Hi There,
I''ve got a threaded discussion tool I''ve made in rails, and I
have it so
when you click on messages it toggles open the message, i.e. reveals the
div including the message body, and also reveals any replies to that
message. I use the following code, inspired by an example in the wiki:
<% if message.resource %>
<%= link_to_remote "»",
:url=>{
:controller=>''resources'',
:action=>''show'',
:id=>message.resource.id,
:params => {:context => ''small''}},
:update=>''artifact'',
:loading=>''Toggle.display(\''message''+message.message_id.to_s+''body\'');Toggle.display(\''message''+message.message_id.to_s+''children\'')''
%>
<%else%>
<%= link_to_remote "»",
:url=>{},
:loading=>''Toggle.display(\''message''+message.message_id.to_s+''body\'');Toggle.display(\''message''+message.message_id.to_s+''children\'')''
%>
<%end%>
However I am repeating myself here and rails is all about DRY. Actually
the main repetition is due to checking whether a message has an
associated resource, and checking whether to load that up as well.
Anyway, my question is should I be constructing the loading string
earlier in the template and referring to it, or building it in the
controller? Is there a rails approved approach here?
Any help much appreciated.
CHEERS> SAM
Sam Joseph wrote:>Hi There, > >I''ve got a threaded discussion tool I''ve made in rails, and I have it so >when you click on messages it toggles open the message, i.e. reveals the >div including the message body, and also reveals any replies to that >message. I use the following code, inspired by an example in the wiki: > ><% if message.resource %> ><%= link_to_remote "»", >:url=>{ >:controller=>''resources'', >:action=>''show'', >:id=>message.resource.id, >:params => {:context => ''small''}}, >:update=>''artifact'', >:loading=>''Toggle.display(\''message''+message.message_id.to_s+''body\'');Toggle.display(\''message''+message.message_id.to_s+''children\'')'' >%> ><%else%> ><%= link_to_remote "»", >:url=>{}, >:loading=>''Toggle.display(\''message''+message.message_id.to_s+''body\'');Toggle.display(\''message''+message.message_id.to_s+''children\'')'' >%> ><%end%> > > >However I am repeating myself here and rails is all about DRY. Actually >the main repetition is due to checking whether a message has an >associated resource, and checking whether to load that up as well. > >Anyway, my question is should I be constructing the loading string >earlier in the template and referring to it, or building it in the >controller? Is there a rails approved approach here? > >Any help much appreciated. > >Howabout some kind of helper toggle_remote_helper All it would need would be a value that saves state (open/closed) and the corresponding loading string for the state Sorry no code :) Kev
Hi Kev,
Great suggestion - turned out to be very easy:
module ApplicationHelper
def toggle (id,prefix,suffix)
''Toggle.display(\''''+prefix+id.to_s+suffix+''\'');''
end
end
module MessagesHelper
def toggleBodyAndChildren(id)
toggle(id,''message'',''body'')+toggle(id,''message'',''children'')
end
end
view is now:
<%= link_to_remote "»",
:url=>{},
:loading=>toggleBodyAndChildren(message.message_id)
%>
Clearly this is the rails way :0)
Many thanks.
CHEERS> SAM
Kev Jackson wrote:
>Howabout some kind of helper
>
>toggle_remote_helper
>
>All it would need would be a value that saves state (open/closed) and
>the corresponding loading string for the state
>
>Sorry no code :)
>
>Kev
>
>
>
>
>