如何从rails 3.2中另一个控制器中的一个控制器调用该方法?

时间:2014-03-23 13:55:57

标签: ruby-on-rails session

我一直在网上搜索最近几个小时,但是找不到如何调用我在另一个控制器中创建的控制器中的一个方法,然后在我的视图中填充我的html页面。

我试图调用的方法就是这个:

class CataloguesController < ApplicationController


  def index
    @catalogues = Catalogue.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @catalogues }
    end

我试图用以下方法调用它:

      def index
    session[:cart].catalogues = Catalogue.all
  end

当我在我的html页面中运行时,Rails没有并且正在返回以下错误。

undefined method `catalogues=' for {:id=>5}:Hash

非常感谢任何帮助...

修改

cart_controller

class CartController < ApplicationController

  before_filter do
    session[:cart] = session[:cart] || {} # set an empty cart if no cart exist
  end



  def index
    session[:cart].catalogues = Catalogue.all
  end

  def success
  end


  def add
    id = params[:id]
    amount = session[:cart][id] || 0 # gets cart amount or 0 if no amount was found
    session[:cart][id] = amount + 1 # set new amount
    @catalogue = Catalogue.find(params[:id])
    session[:cart]  = {:id => @catalogue.id }
    redirect_to action: "index"

    # @catalogue = Catalogue.find(params[:id])
    # session[:cart] = cart
    # cart = {:id => @catalogue.id, :catalogue => @catalogue.name,category => @catalogue.category, :code => @catalogue.code , :colour=> @catalogue.colour, :description => @catalogue.description, :image  => @catalogue.image , :unitprice => @catalogue.unitprice, :unitquantity => @catalogue.unitquantity, :unitweight => @catalogue.unitweight }
    # session[:cart] = cart
    # render 'cart/add'
  end

  def remove 

  end

  def clear
  end

  def checkout

  end

end

0 个答案:

没有答案