Rails 4路由问题,或其他;我现在还不确定

时间:2015-05-18 17:21:43

标签: ruby-on-rails ruby-on-rails-4 controller routes

好的,当我试图在使用脚手架生成的主控制器中渲染它们时,这些仪表板页面最初起作用。我选择尝试将它们分成自己的控制器。我正在从另一个应用程序复制大部分内容,但它是rails 3,我使用的是rails 4.此外,我只有一个模型order.rb,其中生成图表的查询驻留。

routes.rb中:

Rails.application.routes.draw do
  resources :orders do   
    collection do
      get :search, as: 'search'
    end
  end

  namespace :dashboards do
    get 'chart_needby_monthly'
    get 'chart_total_orders_by_qty'
    get 'chart_qty_monthly'
  end

  root 'orders#landing'
end

控制器:

class DashboardsController < ApplicationController

  def chart_needby_monthly
    logger.info 'I’m inside the controller function, the value of params is: #{params([:dyear])}'

    year = ((params[:dyear]).gsub(/\D/,'')

    logger.info 'After assignmnet,  dyear is: #{dyear}'

    @chart_data_one = Order.search_one(year)
  end

  def chart_total_orders_by_qty
    @chart_data_two = Order.search_two(year)
  end

  def chart_qty_monthly    
    @chart_data_three = Order.search_three(year)
  end

  private 
    def chart_needby_monthly_params
      params.require(:dyear)
    end
end

chart_needby_monthly.html.erb

<div align = "center">
  <h3>Please Choose the Year for the Dashboard</h3>
</div>
<div align = "center">
  <%= form_tag dashboards_chart_needby_monthly_path, method: :get do %>
   <%= text_field_tag :dyear, (params[:dyear] || Time.now.year.to_s), :class => "search-query span3", :placeholder => "Enter the Year", :autofocus => "autofocus", :datatoggle => "tooltip", :title => "Enter a Year" %>
  <% end %>
</div>
</div>
</br>
</br>
    <%= column_chart @chart_data_one, {library: {hAxis: {title: "Month"}, vAxis: {title: "Number of Orders"}, title: "Need By (Month/Year)"}} %>

因此,当我尝试导航到该页面时,我得到了500页出错的页面。

我知道我在这里缺少一些东西。

这是意图:

用户在页面顶部的text_field中输入一年并点击进入,然后重新呈现输入年份的页面;它默认为第一次渲染时的当前年份,以便默认情况下将某些内容传递给模型中的查询。

我在3个不同的页面上有三个不同的图表。

以下是服务器日志的输出:

[INFO](null):0(null)():( null):在2015-05-18 10:06:06 -0700开始获取10.63.134.10 GET“/ dashboards / chart_needby_monthly”

所以它试图去做,但它似乎永远不会真正到达控制器。我一直试图解决这个问题4天,我似乎无法弄清楚我做错了什么。

哦,这是路线rake:

                       Prefix Verb   URI        Pattern                                     Controller#Action
                       search_orders GET    /orders/search(.:format)                        orders#search
                              orders GET    /orders(.:format)                               orders#index
                                     POST   /orders(.:format)                               orders#create
                           new_order GET    /orders/new(.:format)                           orders#new
                          edit_order GET    /orders/:id/edit(.:format)                      orders#edit
                               order GET    /orders/:id(.:format)                           orders#show
                                     PATCH  /orders/:id(.:format)                           orders#update
                                     PUT    /orders/:id(.:format)                           orders#update
                                     DELETE /orders/:id(.:format)                           orders#destroy
     dashboards_chart_needby_monthly GET    /dashboards/chart_needby_monthly(.:format)      dashboards#chart_needby_monthly
dashboards_chart_total_orders_by_qty GET    /dashboards/chart_total_orders_by_qty(.:format) dashboards#chart_total_orders_by_qty
        dashboards_chart_qty_monthly GET    /dashboards/chart_qty_monthly(.:format)         dashboards#chart_qty_monthly
                                root GET    /                                               orders#landing

1 个答案:

答案 0 :(得分:1)

您应该尝试在开发环境中重现这一点,这样您就可以看到堆栈跟踪,告诉您到底出了什么问题而不仅仅是出现服务器错误。

在您的控制器操作中,您执行此操作:

year = ((params[:dyear]).gsub(/\D/,'')

但是有一个未公开的括号,所以你最有可能得到语法错误。将其更改为有希望修复它:

year = (params[:dyear]).gsub(/\D/, '')

或者甚至更好,因为你不需要那些初始括号:

year = params[:dyear].gsub(/\D/, '')

但是你在其他控制器操作中又犯了一个错误。在chart_total_orders_by_qtychart_qty_monthly中,您指的是未定义的本地变量year。最简单的解决方法是将其提取到自己的方法中:

class DashboardsController < ApplicationController

  def chart_needby_monthly
    @chart_data_one = Order.search_one(year)
  end

  def chart_total_orders_by_qty
    @chart_data_two = Order.search_two(year)
  end

  def chart_qty_monthly    
    @chart_data_three = Order.search_three(year)
  end

  private
  def year
    params[:dyear].gsub(/\D/, '')
  end
end