开发者

Use Model Method from Different Controller - Rails 3

My application has d开发者_JS百科eals and orders. I'm processing the orders manually by clicking a link my my admin area when viewing the deals.

views/access/deals/index.html.erb

<%= link_to "Process Orders", process_orders_access_deal_path(deal) %>

In my /controllers/access/deals_controller.rb, I have the following method.

def process_orders
    @deal = Deal.find(params[:id])
    @orders = @deal.orders.where("state" == ?, "pending")
    @orders.each do |order|
      order.purchase
    end
 end

My purchase method is not in my deals model, it's in my orders model because I set it up there before needing to make changes where orders would not be processed immediately.

Purchase method in orders controller.

def purchase
  response = Order.gateway.purchase(order_amount, billing_id, options)
end

Is it possible to access the purchase method in my order.rb model from my /controllers/access/deals_controller.rb and if so how?


Have you tried calling it?

Controllers and models aren't linked by some inexplicable magic. You are able to reference any class (i.e. model) from any other class you want (i.e. controller). There is no restriction.


Before using Rails read something about object-oriented programming and classes in Ruby. Please don't take it personal, but you cannot write a book without understanding of alphabet. I know it from my own experiences.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜