# RJS Files, Conditions and REST

**URL:** <https://www.ruby-forum.com/t/rjs-files-conditions-and-rest/152150>\
**Category:** Rails\
**Created:** [October 30, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/rjs-files-conditions-and-rest/152150 "2008-10-30T16:07:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [October 30, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/rjs-files-conditions-and-rest/152150/1 "2008-10-30T16:07:43Z")

</div>

Hey all,

I’m working on a project right now, and one of the models is  
Company… simple regular company.

Now I’ve created some AJAX type searches in my project that rely on  
searching on companies, which I use the INDEX action of  
company\_controller and pass a parameter which returns my companies,  
using an RJS file I do my AJAX… this is what index.js.rjs looks like  
for my company\_controller.

page.replace\_html ‘results’, :partial =\> ‘/search\_results’  
page.visual\_effect :highlight, ‘Flash’

Simple… now where I’m confusing myself is that I use the index  
action to generate a list of companies several times… but the way I  
have ti setup, I’m foreced to render search\_results each time… I’m  
curious what you guys think the best way to get around this is…

putting conditions in the RJS?

if params[:flag] == red  
page.replace\_html ‘results’, :partial =\> ‘/  
search\_results\_no\_widgets’  
else  
page.replace\_html ‘results’, :partial =\> ‘/  
search\_lists\_with\_widgets’  
end

or is the better solution to create a “format” for each one and add  
another format to the index action…

respond\_to do |format|  
format.html # index.html.erb  
format.js #index.js.erb ( widgets )  
format.js2 #index.js2.rjs ( no widgets )  
format.xml { render :xml =\> @companies }  
end

Or is the better solution something else entirely?

Any input on this is appreciated… Thank you.

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [October 30, 2008, 8:03pm UTC](https://www.ruby-forum.com/t/rjs-files-conditions-and-rest/152150/2 "2008-10-30T20:03:11Z")

</div>

Hi,

sw0rdfish wrote:

> I’ve created some AJAX type searches in my project that rely  
> on searching on companies, which I use the INDEX action of  
> company\_controller and pass a parameter

While ‘index’ is an OK choice here, given the default action in Rails,  
you  
might want to consider using the ‘list’ method directly instead.  
‘index’ is  
the default action for any url. What action that actually is a design  
choice.

> else  
> page.replace\_html ‘results’, :partial =\> ‘/  
> search\_lists\_with\_widgets’  
> end

I would definitely recommend this to the other solution you’re  
considering.  
From a ‘personal preference’ perspective, i’d assign params[:flag] to an  
instance variable in the controller. Better decoupling, IMHO.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [October 31, 2008, 7:52pm UTC](https://www.ruby-forum.com/t/rjs-files-conditions-and-rest/152150/3 "2008-10-31T19:52:58Z")

</div>

the LIST command isn’t part of the 7 REST actions… so using list, is  
un-restful!

I’m still not convinced that the if statment in the RJS file is the  
way to go… for fun I made a new RJS file, called it index.js2.rjs  
and made a new MIME type in environment.rb and it seems to work  
well… I’ve got to maintain two RJS files, but to me it makes sense  
because they’re rendering two different views using two different  
partials… the only thing that is the same is the controller action  
that fetches the company…

doing the If in the RJS kinda fells as dirty as putting an IF in the  
view…

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [November 1, 2008, 2:12am UTC](https://www.ruby-forum.com/t/rjs-files-conditions-and-rest/152150/4 "2008-11-01T02:12:19Z")

</div>

Hi,

sw0rdfish wrote:

> the LIST command isn’t part of the 7 REST actions…  
> so using list, is un-restful!

Read more on REST. You’ve come to an incorrect conclusion: if it isn’t  
part of the Rails default support for REST, then it isn’t REST. Utter  
nonsense.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [November 3, 2008, 8:06pm UTC](https://www.ruby-forum.com/t/rjs-files-conditions-and-rest/152150/5 "2008-11-03T20:06:44Z")

</div>

Regardless if I’m using “index” or “list” it still doesn’t solve my  
issue. If both calls were made to the list command, my question is  
would I use a new format to get two different outputs ( views ) or is  
the correct way to use if statements in the RJS files? I’m leaning  
the way of the new format, just cuz it’s easier, and cleaner… my  
call askes for formatted\_company\_path(js2) and I get the index.js2.rjs  
file… makes sense to me… I’m curious to hear from others on the  
drawbacks of doing it this way… and on LIST being RESTful for that  
matter…

I’ll do some more reading, but it’s been my understanding that it is  
not.
