# Ajax

**URL:** <https://www.ruby-forum.com/t/ajax/130893>\
**Category:** Rails\
**Created:** [March 4, 2008, 9:36am UTC](https://www.ruby-forum.com/t/ajax/130893 "2008-03-04T09:36:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [March 4, 2008, 9:36am UTC](https://www.ruby-forum.com/t/ajax/130893/1 "2008-03-04T09:36:09Z")

</div>

Hi  
I have in the controller

def show\_details

@sd\_ticket=ServiceDeskTicket.find(params[:id])  
@sd\_attachments=ServiceDeskAttachment.find\_all\_by\_service\_desk\_ticket\_id(params[:id])  
@servicedesk\_cis\_join = @sd\_ticket.service\_desk\_cis  
@sd\_cis = @servicedesk\_cis\_join.map(&:ci)  
@sd\_cis\_association\_type\_ids =  
@servicedesk\_cis\_join.map(&:service\_desk\_ci\_association\_type\_id)

end

And the view show\_details contains many partial files in tabs for  
displayig each of the above like

- Details
- Attachments
- CIs

\<%= render :partial=\>'service\_desk\_part/view\_sd\_details' %\>

\<%= render :partial =\> 'service\_desk\_part/view\_sd\_attachment\_details' %\>

\<%= render :partial =\> 'service\_desk\_part/view\_sd\_ci\_details' %\>

In \_view\_sd\_attachment\_details.rhtml a for loop there like following(I  
have included only a small portion of the code)

\<% for sd\_attachment in @sd\_attachments %\> //sd\_attachment contains say  
100items

\<%= sd\_attachment.created\_on %\>

What i need is pagination for the above  
\_view\_sd\_attachment\_details.rhtml.And here view\_sd\_details is  
same in all pages like a header information…For example

 Description \<%=h @sd\_ticket.description %\>

How can i do this with custom pagination…And if I use Ajax can I avoid  
show\_details method executing information for view\_sd\_details again and  
again…And how can I do that…(The same thing to view\_sd\_ci\_detils tab  
also)

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [March 5, 2008, 5:37am UTC](https://www.ruby-forum.com/t/ajax/130893/2 "2008-03-05T05:37:38Z")

</div>

Any clarification needed?

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [March 4, 2008, 11:18am UTC](https://www.ruby-forum.com/t/ajax/130893/3 "2008-03-04T11:18:14Z")

</div>

Sijo Kg wrote:

> I have in the controller

I’m going to give your code a slightly aggressive review.

Firstly, I don’t know if our e-mail software took out your indentation,  
but you  
need to indent your blocks, with spaces (and never tabs). def and end  
should be  
over 2, and the lines inside them should be over 4.

> def show\_details
> 
> @sd\_ticket=ServiceDeskTicket.find(params[:id])

In a controller, only use @ if you actually need a variable to live  
longer than  
the current method.

> @sd\_attachments=ServiceDeskAttachment.find\_all\_by\_service\_desk\_ticket\_id(params[:id])

Always put the correct amount of spaces around operators like =, for  
consistent  
readability.

Next, don’t use shortcuts or acronyms for variable names. You don’t need  
‘sd\_’  
as a “registered package prefix” for “show\_details”, because your  
variables  
should either be for everyone, or they should not use @.

Next, ServiceDeskTicket should use a ‘has\_many  
:service\_desk\_attachments’  
directive. Then instead of ‘@sd\_attachments = …’, you can just say  
ticket.service\_desk\_attachments.

> @servicedesk\_cis\_join = @sd\_ticket.service\_desk\_cis  
> @sd\_cis = @servicedesk\_cis\_join.map(&:ci)  
> @sd\_cis\_association\_type\_ids =  
> @servicedesk\_cis\_join.map(&:service\_desk\_ci\_association\_type\_id)

The same critique. All these associations should be declared inside the  
model,  
using either directives like ‘has\_many’ or ‘belongs\_to’, or even simple  
methods  
that return fully cooked data. Then the controller only needs to call  
the few  
methods it needs.

And I suspect your type\_id could go into a polymorphic Model, for even  
more  
transparency. That’s the root principle of Object Oriented Programming,  
so it’s  
also frequently a good idea!

And write unit tests for everything, too!

> \<%= render :partial=\>‘service\_desk\_part/view\_sd\_details’ %\>
> 
> \<%= render :partial =\> 'service\_desk\_part/view\_sd\_attachment\_details' %\>
> 
> \<%= render :partial =\> 'service\_desk\_part/view\_sd\_ci\_details' %\>

Good! Partials tame RHTML!

> In \_view\_sd\_attachment\_details.rhtml a for loop there like following(I  
> have included only a small portion of the code)
> 
> \<% for sd\_attachment in @sd\_attachments %\> //sd\_attachment contains say  
> 100items
> 
> \<%= sd\_attachment.created\_on %\>

You might also try render :partial … :collection.

> What i need is pagination for the above  
> \_view\_sd\_attachment\_details.rhtml.And here view\_sd\_details is  
> same in all pages like a header information…For example
> 
> Description

I thought tr/th should be inside a thead, right?

> ```
> <td width=""><%=h @sd_ticket.description %></td>
> 
> ```
> 
> How can i do this with custom pagination…And if I use Ajax can I avoid  
> show\_details method executing information for view\_sd\_details again and  
> again…And how can I do that…(The same thing to view\_sd\_ci\_detils tab  
> also)

Try this (roughly):

render :partial =\> ‘header’,  
:locals =\> {  
:label =\> ‘Attachments’,  
:ticket =\> @sd\_ticket,  
:rows =\> proc{  
render :partial =\> ‘row’,  
:collection =\> @sd\_ticket.attachments }  
}

Then inside \_header.rhtml use something like…

 \<%= label %\>... \<%= rows.call %\> 

Now \_header.rhtml is completely reusable, and you reuse it by writing  
that big  
render… line again, and changing the values of :label and :rows.

BTW I don’t see no Ajax around here. Good thing, too, because simple  
reports and  
partials should all be raw HTML, right?

–  
Phlip  
[O'Reilly Media - Technology and Business Training](http://www.oreillynet.com/ruby/blog/2008/03/beast_acts_as_sphinx.html)

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [March 4, 2008, 12:12pm UTC](https://www.ruby-forum.com/t/ajax/130893/4 "2008-03-04T12:12:20Z")

</div>

I did not understand

Try this (roughly): //In which of my above file I shud  
write this?

render :partial =\> ‘header’,  
:locals =\> {  
:label =\> ‘Attachments’,  
:ticket =\> @sd\_ticket,  
:rows =\> proc{  
render :partial =\> ‘row’,  
:collection =\> @sd\_ticket.attachments }  
}

Then inside \_header.rhtml use something like…////I have no file  
\_header.rhtml

 \<%= label %\>... \<%= rows.call %\> 

u r right That is a polymorphic relation.And how pagination works  
here.Please answer (I am beginner to Ajax also)…I thought it would done  
by object\_field and all (Please correct me if I am wrong)

Sijo
