# Ajax.updater to call app\_helper or model method

**URL:** <https://www.ruby-forum.com/t/ajax-updater-to-call-app-helper-or-model-method/161689>\
**Category:** Rails\
**Created:** [March 1, 2009, 12:03am UTC](https://www.ruby-forum.com/t/ajax-updater-to-call-app-helper-or-model-method/161689 "2009-03-01T00:03:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 1, 2009, 12:03am UTC](https://www.ruby-forum.com/t/ajax-updater-to-call-app-helper-or-model-method/161689/1 "2009-03-01T00:03:06Z")

</div>

I am quite newbie to rails and stuck with a problem.

In my application I have a Ajax.updater call to update counties list in  
a select drop down whenever a city name is selected. I need to do this  
in many place in my application. Currently the Ajax is working fine. But  
the get\_counties action is in the controller. So I am using this action  
in all controllers that I need. However, it would be nice if I could  
move this to a County or City model or application\_helper.rb . Calling  
this method from application\_helper is not working as rails/ajax still  
looks for this action in the controller. How can I deal with this? And  
in general what will be the best place to add this method.

Below is my code:

## Controller action:

def get\_counties  
@county = {}  
@counties = {}  
@fake\_county\_name = ‘(city)’  
if not params[:id].blank?  
index = params[:id]  
@counties = County.find(:all, :conditions=\> [“city\_id = ? AND name  
!= ?”, index, @fake\_county\_name ], :order=\>“name”)  
end  
render :partial =\> ‘county’, :locals=\>{ “counties”=\>@counties,  
“county”=\>@counties }  
end

* * *

* * *

* * *

## View partial:
 \<%= options\_from\_collection\_for\_select(@counties, "id", "name", @county[:id]) %\> 

| Select city:\* | \<%= options\_from\_collection\_for\_select(@cities,"id","name",@city[:id]) %\> |
| Select county:\* |

* * *

* * *

## \_section.rhtml \<%= options\_from\_collection\_for\_select(@secs, “id”, “name”, @sec[:id]) %\>

Cheers,  
CS.

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 1, 2009, 12:57am UTC](https://www.ruby-forum.com/t/ajax-updater-to-call-app-helper-or-model-method/161689/2 "2009-03-01T00:57:05Z")

</div>

On Feb 28, 11:03 pm, Carlos S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> in general what will be the best place to add this method.

The more conventional way to do this would to have a single  
get\_counties action in the Cities controller (or expose counties as a  
nested resource of cities, but you might want to leave that for  
another day). Your if you change your ajax updater to make a request  
to /some\_controller/some\_action then all of the pages using this  
partial will make their ajax requests to the same place

Fred

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 1, 2009, 1:48am UTC](https://www.ruby-forum.com/t/ajax-updater-to-call-app-helper-or-model-method/161689/3 "2009-03-01T01:48:04Z")

</div>

Thanks Fred…

I don’t have counties or cities controller right now. The  
get\_counties/sections method is placed in the other controllers (users  
and exhibitions).

I wanted to do this application RESTful way, however I haven’t been  
successful in doing so. Your suggestion of making counties as a nested  
resource of cities was my initial thought. (And also user belongs to a  
county. ) However, when I tried to do RESTful the ‘usual CRUD actions’  
(list, add, show, update, delete) for the user\_controller worked but  
get\_counties action did not work. The error being in Ajax.updater method  
as it uses POST and REST allowed only get, put, and delete.

Can someone help me in understanding this?

1. Any suggestion for placing get\_sections method?
2. And how to use actions in RESTful way? Many tutorials point to list,  
add, show, update, delete example… But how can we do it for other  
actions. (In my case essentially it will be a list of counties  
pertaining to particular city… But I don’t know how to do this… Any  
help appreciated…

Thanks,  
CS.

Frederick C. wrote:

> On Feb 28, 11:03ï¿½pm, Carlos S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > in general what will be the best place to add this method.
> 
> The more conventional way to do this would to have a single  
> get\_counties action in the Cities controller (or expose counties as a  
> nested resource of cities, but you might want to leave that for  
> another day). Your if you change your ajax updater to make a request  
> to /some\_controller/some\_action then all of the pages using this  
> partial will make their ajax requests to the same place
> 
> Fred

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 1, 2009, 3:01am UTC](https://www.ruby-forum.com/t/ajax-updater-to-call-app-helper-or-model-method/161689/4 "2009-03-01T03:01:19Z")

</div>

On Mar 1, 12:48 am, Carlos S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> (list, add, show, update, delete) for the user\_controller worked but  
> get\_counties action did not work. The error being in Ajax.updater method  
> as it uses POST and REST allowed only get, put, and delete.

You can use any method with an Ajax.Updater ( check the prototype api  
docs).

> Can someone help me in understanding this?
> 
> 1. Any suggestion for placing get\_sections method?
> 2. And how to use actions in RESTful way? Many tutorials point to list,  
> add, show, update, delete example… But how can we do it for other  
> actions. (In my case essentially it will be a list of counties  
> pertaining to particular city… But I don’t know how to do this… Any  
> help appreciated…

You can have arbitrary actions on both members of a collection or the  
collection itself. I don’t think you need this here though. I would  
recommend you read through the guide on routing (  
[http://guides.rubyonrails.org/routing\_outside\_in.html](http://guides.rubyonrails.org/routing_outside_in.html) )

Fred

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 1, 2009, 3:40am UTC](https://www.ruby-forum.com/t/ajax-updater-to-call-app-helper-or-model-method/161689/5 "2009-03-01T03:40:31Z")

</div>

Frederick C. wrote:

> On Mar 1, 12:48ï¿½am, Carlos S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > (list, add, show, update, delete) for the user\_controller worked but  
> > get\_counties action did not work. The error being in Ajax.updater method  
> > as it uses POST and REST allowed only get, put, and delete.
> 
> You can use any method with an Ajax.Updater ( check the prototype api  
> docs).

If I use get method then it does not update the select dropdown (which  
is what it is intended to do).

> > Can someone help me in understanding this?
> > 
> > 1. Any suggestion for placing get\_sections method?
> > 2. And how to use actions in RESTful way? Many tutorials point to list,  
> > add, show, update, delete example… But how can we do it for other  
> > actions. (In my case essentially it will be a list of counties  
> > pertaining to particular city… ï¿½But I don’t know how to do this… Any  
> > help appreciated…
> 
> You can have arbitrary actions on both members of a collection or the  
> collection itself. I don’t think you need this here though. I would  
> recommend you read through the guide on routing (  
> [http://guides.rubyonrails.org/routing\_outside\_in.html](http://guides.rubyonrails.org/routing_outside_in.html) )
> 
> Fred

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 1, 2009, 3:50am UTC](https://www.ruby-forum.com/t/ajax-updater-to-call-app-helper-or-model-method/161689/6 "2009-03-01T03:50:28Z")

</div>

On Mar 1, 2:40 am, Carlos S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> If I use get method then it does not update the select dropdown (which  
> is what it is intended to do).

Then you must be doing something funny somewhere as (apart from  
routing) whether it’s a get or a post is pretty much irrelevant (and  
semantically get is the correct method to be using here).

Fred

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 1, 2009, 10:13am UTC](https://www.ruby-forum.com/t/ajax-updater-to-call-app-helper-or-model-method/161689/7 "2009-03-01T10:13:39Z")

</div>

It works fine until I start making my application RESTful way…  
Any clues? How can I debug this issue?

Frederick C. wrote:

> On Mar 1, 2:40ï¿½am, Carlos S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > If I use get method then it does not update the select dropdown (which  
> > is what it is intended to do).
> 
> Then you must be doing something funny somewhere as (apart from  
> routing) whether it’s a get or a post is pretty much irrelevant (and  
> semantically get is the correct method to be using here).
> 
> Fred
