如何在Ruby on Rails中访问session [:variable]

时间:2014-04-17 18:25:35

标签: ruby-on-rails ruby session

在我的控制器的方法中,创建我正在尝试将数组推送到session[:arr]

session[:arr] ||= Array.new

@params ||={
    ids: appliance_params[:ids],
    status: appliance_params[:status]
} 
session[:arr] << @params

如果我在create方法中写puts session[:arr],它会显示我的数组, 但我定义了自己的方法appinfo,我希望在此显示puts session[:arr], 但它什么都没给我。 我做错了什么?

代码清单:

  class AppliancesController < ApplicationController
  before_action :set_appliance, only: [:show, :edit, :update, :destroy]
  # before_filter :authenticate_user!
  skip_before_filter :verify_authenticity_token, only: [:appinfo]


  # GET /appliances
  # GET /appliances.json

  def appinfo
    a ||= session[:arr]
    puts a #nothing

    u = User.find_by(apikey: appliance_params[:apikey], id: appliance_params[:id])
    if u
      render json: session[:arr]
      #session[:arr] = nil
      puts session[:arr]
    else 
      render json:  "UserError : error, i haven't found such user with this params :("
    end
  end

  def index
    #@appliances = Appliance.all
    @appliances = current_user.appliances
  end

  # GET /appliances/1
  # GET /appliances/1.json
  def show
  end

  # GET /appliances/new
  def new
    @appliance = Appliance.new
  end

  # GET /appliances/1/edit
  def edit
  end

  # POST /appliances
  # POST /appliances.json
  def create
    #@appliance = Appliance.new(appliance_params)
    @appliance = current_user.appliances.new(appliance_params)

    respond_to do |format|
      if @appliance.save
        format.html { redirect_to @appliance, notice: 'Appliance was successfully created.' }
        format.json { render action: 'show', status: :created, location: @appliance }
      else
        format.html { render action: 'new' }
        format.json { render json: @appliance.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /appliances/1
  # PATCH/PUT /appliances/1.json
  def update 
    session[:arr] ||= Array.new

    @params ||={
        ids: appliance_params[:ids],
        status: appliance_params[:status]
    } 
    session[:arr] << @params
    puts session[:arr]

    respond_to do |format|
      if @appliance.update(appliance_params)
        format.html { redirect_to @appliance, notice: 'Appliance was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @appliance.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /appliances/1
  # DELETE /appliances/1.json
  def destroy
    @appliance.destroy
    respond_to do |format|
      format.html { redirect_to appliances_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_appliance
      @appliance = Appliance.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def appliance_params
      params.require(:appliance).permit(:ids, :status, :voltage, :appliance_type ,:apikey, :id)
    end
end

0 个答案:

没有答案