Rails 4:使用AJAX更新实例变量

时间:2016-12-12 17:21:13

标签: jquery ruby-on-rails ajax ruby-on-rails-4 instance-variables

我有一个搜索模型,用于从我的数据库中提取@consult_stats(在RailsCasts之后)。每个搜索都是搜索模型中的帖子,@search.consult_stats

找到给定搜索的有意义结果
class Search < ActiveRecord::Base
    def consult_stats
        @consult_stats ||= find_consult_stats
    end

    private

    def find_consult_stats
        consult_stats = ConsultStat.all
        puts consult_stats.ids, 1
        consult_stats = consult_stats.joins(:weekly_report).where("weekly_reports.location_id = ?", location_id) if location_id.present?
        puts consult_stats.ids, 2
        consult_stats = consult_stats.joins(:weekly_report).where("weekly_reports.start_date >= ?", start_date) if start_date.present?
        puts consult_stats.ids, 3
        consult_stats = consult_stats.joins(:weekly_report).where("weekly_reports.start_date <= ?", end_date) if end_date.present?
        puts consult_stats.ids, 4
        consult_stats = consult_stats.where(employee_id: employee_id) if employee_id.present?

        consult_stats
    end
end

以下是控制人员:

class SearchesController < ApplicationController
def create
    @search = Search.new(search_params)
    respond_to do |format|
      if @search.save
        format.html { redirect_to @search, notice: 'Search was successfully created.' }
        format.json { render :show, status: :created, location: @search }
      else
        format.html { render :new }
        format.json { render json: @search.errors, status: :unprocessable_entity }
      end
   end
end
end

def show
    respond_to do |format|
        format.json do
            @search = Search.find(params[:id])
            @consult_stats = @search.consult_stats
        end
    end
end

class LocationsController < ApplicationController
    def dashboard
        @columns = ["consults", "signed", "weeks", "extensions", "paid_in_full", "revenue", "revenue_on_account", "signed_stats"]
        @locations = Location.real
        @consult_stats = ConsultStat.all

        @revenues = []
        @employees = Employee.active.order("name ASC")
        @locations.each do |location|
            @revenues << [location.name, ConsultStat.joins(:weekly_report).where("weekly_reports.location_id = ?", location.id).sum(:revenue)]
        end
    end
end

这很好用但是当我从另一个控制器(LocationsController)发出我的ajax请求,并尝试检索@consult_stats时,实例变量(@consult_stats)没有得到更新。相反,@consult_stats会保留locations_contollerConsultStat.all)的价值。

q = "/searches"
        $.ajax({
            url: q, 
            type: "post",
            data: {
                location_id: loc_id,
                start_date: start_date,
                end_date: end_date,
                employee_id: emp_id
            },
            success: function(data) {
                console.log("mememe", data)
                nums = []
                console.log(data) // Displays correct data!
                console.log(<%= @consult_stats %>) // Does not get updated
                $("#consult_stats").text("");

                $("#consult_stats").append("<%= escape_javascript(render 'consult_stats/consult_stat', object: @consult_stats) %>");
            },
            error: function(xhr) {
                console.log(xhr)
            },
            datatype: 'script'
        })
    })

consult_stat partial

<%= @consult_stats.present? ? @consult_stats.length : 0 %>

@consult_stats是否因为在search_contoller中更新,然后在locations_controller中还原?

而未更新

0 个答案:

没有答案