Routes work in Development But not in Production - ruby-on-rails

Weird Error. I have some routes that work perfectly during development but once i deploy and try to access them it comes up with page does not Exist error
I have the following routes.rb file:
TransportUnl::Application.routes.draw do
resources :trucks
resources :shipments do
collection do
get :autocomplete_location_cs
end
end
devise_for :users do
get '/users/sign_in' => 'devise/sessions#new'
get '/users/sign_out' => 'devise/sessions#destroy'
end
root :to => 'info#index'
resources :info do
collection do
get 'about'
get 'contact'
get 'you_dont_have_a_full_account'
get 'help'
end
member do
get 'index'
end
end
resources :companies
end
Not everything is setup yet. but i am getting a page not found error when i go to:
www.website.com/shipments
www.website.com/trucks
as well as others in production. The main index page works and you can login but these pages come up not found.
Production
Development
Production.log
Started GET "/shipments" for 108.235.52.160 at 2015-06-22 13:09:03 -0500
Processing by ShipmentsController#index as HTML
[1m[35m (0.6ms)[0m SELECT MAX("shipments"."price") AS max_id FROM "shipments"
[1m[36mShipment Load (0.3ms)[0m [1mSELECT "shipments".* FROM "shipments" [0m
Rendered shipments/_nav.html.erb (0.6ms)
Rendered shipments/_search_table.html.erb (0.1ms)
Rendered shipments/index.html.erb within layouts/application (1.2ms)
[1m[35mUser Load (0.5ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Rendered shared/_header.html.erb (5.0ms)
Completed 500 Internal Server Error in 12ms
ActionController::RoutingError (No route matches {:action=>"edit", :controller=>"companies"}):
app/views/shared/_header.html.erb:78:in `_app_views_shared__header_html_erb___2847381188393053217_232073740'
app/views/layouts/application.html.erb:17:in `_app_views_layouts_application_html_erb__4421904906041360553_230384600'
app/controllers/shipments_controller.rb:7:in `index'
Started GET "/info/about" for 157.55.39.229 at 2015-06-22 13:10:29 -0500
Processing by InfoController#about as */*
Rendered info/about.html.erb within layouts/application (5.7ms)
Rendered shared/_header.html.erb (3.8ms)
[1m[36m (4.3ms)[0m [1mSELECT * FROM geometry_columns WHERE f_table_name='shipments'[0m
[1m[35mShipment Load (0.9ms)[0m SELECT "shipments".* FROM "shipments" ORDER BY id DESC LIMIT 3
Rendered shared/_footer.html.erb (31.6ms)
Completed 200 OK in 177ms (Views: 151.1ms | ActiveRecord: 25.6ms)
the Link is created like this:
<%= link_to "My Account", edit_company_path(current_user.company_id) %>

As you can see from logs, you got error page because of line 78 of app/views/shared/_header.html.erb file.
In this piece of code, where you creating link
<%= link_to "My Account", edit_company_path(current_user.company_id) %>
Check if company_id is not nil for that particular user. I'm pretty sure it's nil in your case.
As you can see from error logs, it tries to get action edit as collection action of companies controller - companies/edit. You don't have this route defined. But if current user will have company_id, the link will be built correctly and you wont receive an error.

Related

Rails - Couldn't find PackageRequest with 'id'=item_categories

I have a package request model that handles all the tool loaning data. I am trying to have it so that when I go to the package request page in the application, that it shows only the form and not the index. So in the package request controller I tried to redirect the index path to the form path.
def index
#package_requests = PackageRequest.all
redirect_to new_package_request_path
end
But now when I try to go to a different page such as item categories from the package request form page I get this
error.
This kind of error shows up whenever I try to go to a different page from a form or show page.
log of server requests:
Started GET "/package_requests/new" for 127.0.0.1 at 2017-04-03 18:43:55 -0500
Processing by PackageRequestsController#new as HTML
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
User Load (0.1ms) SELECT "users".* FROM "users"
ItemLocation Load (0.1ms) SELECT "item_locations".* FROM "item_locations"
Item Load (0.1ms) SELECT "items".* FROM "items"
Rendered package_requests/_form.html.erb (23.0ms)
Rendered package_requests/new.html.erb within layouts/application (24.1ms)
Rendered shared/_navbar.html.erb (0.1ms)
Completed 200 OK in 60ms (Views: 58.6ms | ActiveRecord: 0.4ms)
Started GET "/package_requests/items" for 127.0.0.1 at 2017-04-03 18:44:01 -0500
Processing by PackageRequestsController#show as HTML
Parameters: {"id"=>"items"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
PackageRequest Load (0.0ms) SELECT "package_requests".* FROM "package_requests" WHERE "package_requests"."id" = ? LIMIT 1 [["id", 0]]
Completed 404 Not Found in 2ms (ActiveRecord: 0.1ms)
ActiveRecord::RecordNotFound (Couldn't find PackageRequest with 'id'=items):
app/controllers/package_requests_controller.rb:68:in `set_package_request'
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (3.2ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.6ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.6ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (46.2ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (0.3ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.2ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.2ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.2ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (40.0ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (0.2ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.4ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (89.4ms)
routes.rb
Rails.application.routes.draw do
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
get "home", to: "static_pages#home"
root 'home#home'
resources :package_requests
resources :zips
resources :countries
resources :states
resources :cities
resources :item_statuses
resources :item_categories
resources :buildings
resources :tag_types
resources :vendors
resources :items
resources :item_locations
resources :loan_histories
resources :order_requests
resources :package_requests
resources :home
devise_for :users, controllers: {registrations: "registrations"}

500 (Internal Server Error) when i display the data from the variable in rails

I got this error.
GET http://localhost:3000/activities/undefined 500 (Internal Server Error)
In browser console log.
When i tried to display the data by this variable in my .erb template file.
<h1><%= #activity.name %></h1>
This is my controller method.
def activities_details
#activity = Activity.find_by(id: params[:id])
#temple = Temple.find_by(id: #activity.temple_id)
render :template => 'front_pages/activity_details'
end
This page still works fine but i still confusing why this error keeps happening.
#
I found the reason it seems like rails call
get request from my link twice.
First time is the correct request,but second time is the wrong one.
this is log from the server
Started GET "/activities/24" for ::1 at 2016-02-22 14:19:13 +0800
Processing by FrontPagesController#activities_details as HTML
Parameters: {"id"=>"24"}
Activity Load (0.4ms) SELECT "activities".* FROM "activities" WHERE "activities"."id" = $1 ORDER BY "activities"."created_at" DESC LIMIT 1 [["id", 24]]
Temple Load (0.4ms) SELECT "temples".* FROM "temples" WHERE "temples"."id" = $1 ORDER BY "temples"."created_at" DESC LIMIT 1 [["id", 7]]
Rendered front_pages/activity_details.html.erb within layouts/application (0.3ms)
Rendered shared/_header.html.erb (3.8ms)
Rendered shared/_footer.html.erb (0.1ms)
Completed 200 OK in 419ms (Views: 414.3ms | ActiveRecord: 0.8ms)
Started GET "/assets/shutterstock_96393731.jpg" for ::1 at 2016-02-22 14:19:13 +0800
Started GET "/activities/undefined" for ::1 at 2016-02-22 14:19:13 +0800
Processing by FrontPagesController#activities_details as HTML
Parameters: {"id"=>"undefined"}
Activity Load (0.6ms) SELECT "activities".* FROM "activities" WHERE "activities"."id" = $1 ORDER BY "activities"."created_at" DESC LIMIT 1 [["id", 0]]
Completed 500 Internal Server Error in 10ms (ActiveRecord: 0.6ms)
NoMethodError (undefined method `temple_id' for nil:NilClass):
app/controllers/front_pages_controller.rb:36:in `activities_details'
This is my link from the previous page.
<%= link_to "Details", activity_show_path(activity.id) ,class: "btn_1"%>
My routes
get '/activities/:id' => 'front_pages#activities_details', :as => 'activity_show'
Thanks!

localhost:3000/topics/index and localhost:3000/topics/show BOTH route to same show.html file

For some reason both these URLS are routing to the same file when they shouldn't be, another thing that I noticed when typing in an invalid url such as localhost:3000/topics/inexjojvnsjg it just stays on the same page.
here is what my rails console is telling me when I try to access the url
localhost:3000/topics/index
Started GET "/topics/index" for ::1 at 2015-02-06 17:33:07 -0700
Processing by TopicsController#show as HTML
Parameters: {"id"=>"index"}
Rendered topics/show.html.erb within layouts/application (0.1ms)
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" =$1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Completed 200 OK in 98ms (Views: 96.5ms | ActiveRecord: 0.8ms)
here is my routes file....
Rails.application.routes.draw do
devise_for :users
get 'welcome/index'
get 'welcome/about'
# get "topics/index"
# get "topics/show"
# get "topics/new"
# get "topics/edit"
#for some reason, using resources:topics, index and show both route to show
resources :topics
root to: 'welcome#index'
post :incoming, to: 'incoming#create'
end
Here is the key info:
Started GET "/topics/index" for ::1 at 2015-02-06 17:33:07 -0700
Processing by TopicsController#show as HTML
Parameters: {"id"=>"index"}
The :index url for a TopicsController is "/topics".
The :show url for a TopicsController is "/topics/:id" or "/topics/1", where the last part of the url gets associated to the params[:id]. With the url "/topics/1" the :id = 1.
So when you go to the url "/topics/index" you are going to the :show action because of the "index" part of the url. You are just setting the :id to "index" instead of a Integer :id. You can see that in the output you pasted here:
Parameters: {"id"=>"index"}
TLDR: "/topics/index" is a route the will pass the Rails router but is an invalid route, because the :id is a String "index".

Even though I have a scoped nested route, my resources are not routing properly - Rails 3.1

This is my route:
scope ":username" do
resources :feedbacks
end
So when I go to mydomain.com/test/feedbacks/10 it shows the correct feedback with id=10 that belongs to username=test.
But, if I go to mydomain.com/test2/feedbacks/10 it shows me the same feedback with id=10, which does NOT belong to username=test2.
How do I restrict this from happening?
I am using the Vanity gem to give me the username in the URL, this is what that route looks like:
controller :vanities do
match ':vname' => :show, :via => :get, :constraints => {:vname => /[A-Za-z0-9\-\+\#]+/}
end
Edit 1:
That is to say, for clarity's sake, when I go to mydomain.com/test/feedbacks/10 and /test2/feedbacks/10, it shows me the same view for the same record (in which case, the latter version would be wrong because it should be telling me that no such record exists, but it's not. It is just displaying the correct record for test/feedbacks/10).
Edit 2:
Here are the logs of both requests:
The right request
Started GET "/test-3/feedbacks/7" for 127.0.0.1 at 2011-09-14 02:48:15 -0500
Processing by FeedbacksController#show as HTML
Parameters: {"username"=>"test-3", "id"=>"7"}
Feedback Load (0.5ms) SELECT "feedbacks".* FROM "feedbacks" WHERE "feedbacks"."id" = ? LIMIT 1 [["id", "7"]]
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1
Rendered feedbacks/show.html.erb within layouts/application (36.2ms)
Completed 200 OK in 188ms (Views: 184.3ms | ActiveRecord: 1.8ms)
The wrong request
Started GET "/test2/feedbacks/7" for 127.0.0.1 at 2011-09-14 02:48:28 -0500
Processing by FeedbacksController#show as HTML
Parameters: {"username"=>"test2", "id"=>"7"}
Feedback Load (0.1ms) SELECT "feedbacks".* FROM "feedbacks" WHERE "feedbacks"."id" = ? LIMIT 1 [["id", "7"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1
Rendered feedbacks/show.html.erb within layouts/application (37.6ms)
Completed 200 OK in 50ms (Views: 47.5ms | ActiveRecord: 1.2ms)
Your show action should look something like
def show
#user = User.find_by_username(params[:username])
if #user == current_user
...
render "show"
else
flash[:alert] = "Record doesn't exist"
redirect_to root_path
end
end
I took the liberty of adding in #Benoit's suggestion.

ActionController::RoutingError (No route matches "/users"):

I'm using rails 3 + devise. When a user signs up and there is an error. The app is redirecting to:
http://localhost:3000/users
Showing the error messages, but in the log I see the following:
Started POST "/users" for 127.0.0.1 at Mon Jul 11 20:22:19 -0700 2011
Processing by RegistrationsController#create as HTML
Parameters: {"commit"=>"Create my account", "fb_access_token"=>"XXXXX", "authenticity_token"=>"fWd/XXXX=", "utf8"=>"✓", "user"=>{"remember_me"=>"0", "lname"=>"XXXX", "fname"=>"XXXX", "password"=>"[FILTERED]", "email"=>"XXXX-inc.com"}, "fb_uuid"=>"312312"}
SQL (0.1ms) BEGIN
User Load (0.2ms) SELECT "users"."id" FROM "users" WHERE ("users"."email" = 'brett#companyline-inc.com') LIMIT 1
SQL (0.2ms) ROLLBACK
Rendered layouts/_header.html.erb (1.1ms)
Rendered registrations/new.html.erb within layouts/unauthorized (11.3ms)
Completed 200 OK in 45ms (Views: 22.6ms | ActiveRecord: 2.7ms)
Started GET "/users" for 127.0.0.1 at Mon Jul 11 20:22:20 -0700 2011
ActionController::RoutingError (No route matches "/users"):
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.8ms)
routes.rb:
# OmniAuth - for FB Connect
match '/users/auth/facebook/callback' => 'authentications#create'
# Devise
devise_for :users, :controllers => {:registrations => "registrations"}
Why the actionController error?
What does rake routes show you? The order in which you place your routes matters also, since the Rails router picks the first route it matches.
Hope this helps!

Resources