# Right way to do rjs off a method with its own view?

**URL:** <https://www.ruby-forum.com/t/right-way-to-do-rjs-off-a-method-with-its-own-view/130673>\
**Category:** Rails\
**Created:** [March 1, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/right-way-to-do-rjs-off-a-method-with-its-own-view/130673 "2008-03-01T17:23:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [March 1, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/right-way-to-do-rjs-off-a-method-with-its-own-view/130673/1 "2008-03-01T17:23:11Z")

</div>

Hi, just looking for “best practice” guidance…

I have a controller method that renders its output with an rhtml view  
file. The output can also be updated via an rjs update via an rjs view  
file.

I can’t work out if it is possible to have action.rhtml AND action.rjs  
and have the correct one called depending on the request. SO, I have  
done this:

if I have have “my\_action.rhtml” and “my\_action\_update.rjs”,  
then the controller method is :-

def my\_action  
… action code…

```
render :action => :my_action_update if request.xhr?

```

end

This works, but is it the right way, or is there a better way?

Comments appreciated 🙂

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [March 1, 2008, 6:41pm UTC](https://www.ruby-forum.com/t/right-way-to-do-rjs-off-a-method-with-its-own-view/130673/2 "2008-03-01T18:41:33Z")

</div>

John L. napisa³(a):

> ## if I have have “my\_action.rhtml” and “my\_action\_update.rjs”, Comments appreciated 🙂
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Try “respond\_to” -  
[Peak Obsession](http://api.rubyonrails.com/classes/ActionController/MimeResponds/InstanceMethods.html#M000206).

def action  
…action code…  
respond\_to do |format|  
format.html # renders action.rhtml  
format.js # renders action.rjs  
end  
end

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [March 2, 2008, 10:40am UTC](https://www.ruby-forum.com/t/right-way-to-do-rjs-off-a-method-with-its-own-view/130673/3 "2008-03-02T10:40:15Z")

</div>

> Try “respond\_to” -

Yes this worked, thanks!

Actually, it didn’t work straight off. I discovered it only works if  
the files are called “action.html.rhtml” and “action.js.rjs” instead of  
“action.rhtml” and “action.rjs”. I googled and found that this naming  
dependency was introduced with rails 2.0.2.
