从rails中的另一个控制器调用方法

时间:2015-11-23 07:04:56

标签: ruby-on-rails

我有2个控制器(bookingtaxi)。我将在taxi的创建方法中调用booking方法。

所以我看到了this,并使用了这一行

class BookingsController < ApplicationController
      def create
        redirect_to url_for(:controller => :taxis, :action => :compute) and return
    end
end

我添加and return because我面对的是erorr

AbstractController::DoubleRenderError

和计算方法是一个简单的put

def compute  
   puts "Address is @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"     
end

问题是,当我运行它时,我会面对这条消息

Redirected to http://agile-system-lashkarara.c9users.io:8081/compute

但它在控制台中没有任何内容

1 个答案:

答案 0 :(得分:0)

将计算机移动到Taxi模型,如下所示:

#model/taxi.rb
#please note that this is a class method and not an instance method!
def self.compute

  puts "Address is @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@        "

end

然后你可以拨打Taxi.compute,但这可能不是你想要的,因为你可能需要为特定的出租车调用该方法,然后你不需要自己。部分,您需要在出租车的ActiveRecord对象上调用该方法。

相关问题