Shenanigator
Shenanigator

Reputation: 1066

Rails 4 routing issue, or something else; I am not sure at this point

Okay, these dashboard pages originally worked when I was trying to render them within the main controller generated with scaffold. I opted to try and split them off into their own controller. I am copying much of this from another app, but it is rails 3 and I am using rails 4. Also I only have a single model order.rb where the queries for generating the charts resides.

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

Controller:

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)"}} %>

So when I try and navigate to that page I get the 500 something went wrong page.

I know there is some stuff I am missing here.

Here is the intent:

user enters a year in the text_field at the top of the page and hits enter and it re-renders the page for the year entered; it defaults to the current year on first render so that it passes something to the query in the model by default.

I have three different charts on 3 different pages.

Here is the output into the log for the server:

[INFO] (null):0 (null)(): (null): Started GET "/dashboards/chart_needby_monthly" for 10.63.134.10 at 2015-05-18 10:06:06 -0700

So it is trying to do the get, but it never seems to actually get to the controller. I have been trying to figure this out for 4 days and I can't seem to figure out what I have done wrong.

Oh and here is the routes 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

Upvotes: 0

Views: 25

Answers (1)

fivedigit
fivedigit

Reputation: 18702

You should try and reproduce this on a development environment so you can see the stacktrace, telling you exactly what went wrong rather than just getting a server error.

In your controller action you're doing this:

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

But there's an unclosed parenthesis, so you're most likely getting a syntax error. Change it to this to hopefully fix it:

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

Or even better, this, since you don't need those initial parenthesis there:

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

You've got another mistake in other controller actions though. In chart_total_orders_by_qty and chart_qty_monthly you're referring to an undefined local variable year. The easiest fix is to extract it into its own method:

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

Upvotes: 1

Related Questions