开发者

Is it possible to call a 'remote_function' method in helper file instead of controller using Ruby on Rails?

开发者 https://www.devze.com 2023-02-08 19:46 出处:网络
I am using Ruby on Rails 3. I am trying to use the remote_function and I would like to call a method from my helper instead of the controller.

I am using Ruby on Rails 3. I am trying to use the remote_function and I would like to call a method from my helper instead of the controller.

So, in my view file I have:

<%= 
    f.text_field :name, :id => "name_id",
      :onkeyup => remote_function(
        :update => "name_div",
        :url => { :action => :method_name }
%>

That, actually, will ca开发者_如何学运维ll the following method from the controller file (I properly set routers, as well):

def method_name
  ...
end

I would like to move the method_name code in my helper file and then call that method from the helper instead of controller (without setting router). Is it possible using remote_function? If so, how to do that?


Unfortunately, it isn't possible to use remote_function to call a helper method directly. All remote_function does is generate some JavaScript that can make an Ajax request to a particular URL (from the client side). For that URL to work, the route and controller action have to be set up to respond to it.

If your goal is to move the code out of the controller, you could put the action method into a module and include that module in one or more controller classes. You would still need the routes set up to point to that method/action in each controller, however.

0

精彩评论

暂无评论...
验证码 换一张
取 消