无法用Ajax刷新div,我做错了什么?

时间:2012-09-24 15:47:25

标签: ruby-on-rails ajax html refresh

我在div中放置了一个饼图,并希望通过在网页上按两个链接(计数或总计)中的一个来重新计算提供它的数据。

以下是我的观点摘录:

<%= link_to "Count", {action: "switch_chart_dim"}, remote:true %>
<%= link_to "Total", retailers_switch_dim_path("total"), remote:true %>
<div id="visualization" > 
    <%= render partial: '/charts/highcharts_pie', locals: {pie_data: @pie_gender} %>
</div>

我的控制器:

def switch_chart_dim(type=:count)
    logger.debug("URRAY i am here")
    @pie_gender = Receipt.group_by_gender_pie(current_user, type,
                                              params["/retailers"][:begin_date], 
                                              params["/retailers"][:end_date])
    respond_to do |format|
        format.js { render :layout=>false }
    end
end

我的js文件(switch_chart_dim.js.erb)

$('#visualization').html('<%= escape_javascript(render(:partial: '/charts/highcharts_pie', locals: {pie_data: @pie_gender})).html_safe %>')

我已经阅读了几篇关于此的帖子,并尝试将其全部应用,但由于某种原因,似乎我的控制器操作甚至没有被调用(至少我看不到我的“URRAY”调试消息日志)。

我做错了什么?

修改

这是我的匹配路线,如果它可能是相关的:

match '/retailers', to: 'retailers#switch_chart_dim', via: :post, as: "retailers_switch_dim"

1 个答案:

答案 0 :(得分:0)

我似乎发现了这个问题。

我的路由文件有两条冲突的路由,它正在调用搜索操作。我通过分析Firebug中的请求找到了这个:

match '/retailers', to: 'retailers#search', via: :post, as: "retailers_search"
match '/retailers', to: 'retailers#switch_chart_dim', via: :post, as: "retailers_switch_dim"

通过替换

解决了这个问题
match '/retailers', to: 'retailers#switch_chart_dim', via: :post, as: "retailers_switch_dim" 

match '/retailers/switch_chart_dim', to: 'retailers#switch_chart_dim', via: :post, as: "retailers_switch_dim"