redirect_to is undefined - ruby-on-rails

I'm trying to use a redirect_to in my controller and I'm getting an error complaining that redirect_to is not defined (please see logs below)
=> Booting Puma
=> Rails 5.2.1 application starting in development
=> Run rails server -h for more startup options Puma starting in single mode...
* Version 3.12.0 (ruby 2.5.1-p57), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop Started GET "/checkout" for 127.0.0.1 at 2018-11-22 13:18:56 -0800 (0.1ms)
SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY
"schema_migrations"."version" ASC ↳
/var/lib/gems/2.5.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
ActionController::RoutingError (undefined method `redirect_to' for
ChargesController:Class):
app/controllers/charges_controller.rb:39:in
`<class:ChargesController>' app/controllers/charges_controller.rb:3:in
`<main>'
My code is below. I'm not sure what I'm doing wrong. How do I go about debugging this? Any help would be appreciated.
class ChargesController < ApplicationController
def create
redirect_to(:controller => 'home_controller', :action => "index")
end
In my rails routes.rb I have
get "/charge", to: "charges#create"

Related

Rails calling deleted Controller

Basic Rails problem here:
I have a controller method named fetch_headlines that I've been trying to debug. I make changes to the controller and they are not reflected in the output. This has made me think that Rails is not properly calling fetch_headlines. Observe the console below.
Puma starting in single mode...
* Version 3.12.1 (ruby 2.6.1-p33), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
Started GET "/entries/fetch_headlines" for ::1 at 2020-01-08 16:03:03 -0500
(0.6ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
↳ /Users/ed/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
Processing by EntriesController#show as HTML
Parameters: {"id"=>"fetch_headlines"}
Rendering entries/show.html.erb within layouts/application
Rendered entries/show.html.erb within layouts/application (2.8ms)
Completed 200 OK in 482ms (Views: 476.3ms | ActiveRecord: 0.0ms)
Additionally, Rails is presenting the same behavior if I request methods that don't exist such as: qwertyqwerty. I'd expect Rails to throw an error.
Started GET "/entries/qwertyqwerty" for ::1 at 2020-01-09 05:59:13 -0500
Processing by EntriesController#show as HTML
Parameters: {"id"=>"qwertyqwerty"}
Rendering entries/show.html.erb within layouts/application
Rendered entries/show.html.erb within layouts/application (0.6ms)
Completed 200 OK in 18ms (Views: 17.1ms | ActiveRecord: 0.0ms)
Here is my routes.rb
get 'fetch_headlines', to: 'entries#fetch_headlines'
resources :entries
resources :keywords
resources :networks
Thanks.
You put get 'fetch_headlines', to: 'entries#fetch_headlines', it creates /fetch_headlines instead of /entries/fetch_headlines route. What you need is rather something like this:
get 'entries/fetch_headlines', to 'entries#fetch_headlines'
or even better
resources :entries do
collection do
get :fetch_headlines
end
end

Activeadmin 1.3.2 issues in rails 5.2.1

I am using the active admin gem into my new project with Ruby 2.5.1p57 and Rails 5.2.1. All are good. But, I am facing the issue with open new resource link or update the resource. Even, If I add permit_params, same issue.
Started GET "/admin/admin_users/new" for 127.0.0.1 at 2018-10-04 20:59:08 +0530
Processing by Admin::AdminUsersController#new as HTML
AdminUser Load (0.5ms) SELECT `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 1 ORDER BY `admin_users`.`id` ASC LIMIT 1
↳ /home/vivek/.rvm/gems/ruby-2.5.1#regroup2/bundler/gems/activeadmin-
c301ab126b3f/lib/active_admin/base_controller.rb:39
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.5ms)
ArgumentError (wrong number of arguments (given 2, expected 0..1)):
But finally, Fixed the issue by adding below line code to the following
file:
config/initializers/active_admin.rb
def resource_params
[(params[resource_request_name] || params[resource_instance_name]).try(:permit!) || {}]
end
Not sure. Is this correct way to do.

Cannot deploy rails Dynamic router on Heroku

I am doing a redirection based on a model attributes.
It works fine in local but Heroku send me a debug error when I push then finish deploy.
What is that wrong with my code below ? (from here)
2018-07-27T11:18:21.949383+00:00 app[web.1]: D, [2018-07-27T11:18:21.076083 #4] DEBUG -- : Passenger Load (1.3ms) SELECT "passengers".* FROM "passengers"
2018-07-27T11:18:21.949385+00:00 app[web.1]: Puma starting in single mode...
2018-07-27T11:18:21.949384+00:00 app[web.1]: D, [2018-07-27T11:18:21.893911 #4] DEBUG -- : Passenger Load (1.6ms) SELECT "passengers".* FROM "passengers"
2018-07-27T11:18:21.949393+00:00 app[web.1]: * Version 3.12.0 (ruby 2.4.1-p111), codename: Llamas in Pajamas
2018-07-27T11:18:21.949396+00:00 app[web.1]: * Environment: production
2018-07-27T11:18:21.949395+00:00 app[web.1]: * Min threads: 5, max threads: 5
2018-07-27T11:18:21.949532+00:00 app[web.1]: * Listening on tcp://0.0.0.0:50520
2018-07-27T11:18:21.950026+00:00 app[web.1]: Use Ctrl-C to stop
2018-07-27T11:18:22.440272+00:00 heroku[web.1]: State changed from starting to up
2018-07-27T11:21:16.609823+00:00 heroku[web.1]: Restarting
2018-07-27T11:21:16.610597+00:00 heroku[web.1]: State changed from up to starting
2018-07-27T11:21:17.485929+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2018-07-27T11:21:17.501136+00:00 app[web.1]: - Gracefully stopping, waiting for requests to finish
2018-07-27T11:21:17.517026+00:00 app[web.1]: Exiting
2018-07-27T11:21:17.517896+00:00 app[web.1]: /app/vendor/bundle/ruby/2.4.0/gems/puma-3.12.0/lib/puma/server.rb:430:in `close': stream closed (IOError)
2018-07-27T11:21:17.517967+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.4.0/gems/puma-3.12.0/lib/puma/server.rb:433:in `handle_servers'
2018-07-27T11:21:17.517939+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.4.0/gems/puma-3.12.0/lib/puma/server.rb:430:in `ensure in handle_servers'
2018-07-27T11:21:17.517991+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.4.0/gems/puma-3.12.0/lib/puma/server.rb:358:in `block in run'
2018-07-27T11:21:17.608339+00:00 heroku[web.1]: Process exited with status 1
If I try to do a db:migrate it throws
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "passengers" does not exist
LINE 1: SELECT "passengers".* FROM "passengers"
app/models/dynamic_router.rb which contains the faulty loop passenger.all.each do..end which is supposed to work ?
class DynamicRouter
def self.load
Rails.application.routes.draw do
# faulty part
Passenger.all.each do |pg|
get "/#{pg.shortcut}" => redirect("/users/sign_up?q=#{pg.id}&t=#{pg.token}")
end
#end of faulty part
end
end
def self.reload
Rails.application.routes_reloader.reload!
end
end
app/models/passenger.rb
after_save :reload_routes
def reload_routes
DynamicRouter.reload
end
config/routes.rb
Rails.application.routes.draw do
...
DynamicRouter.load
end
It was because Heroku do not accept database calls from route.config
Found the solution here
get ":shortcut" => redirect("/users/sign_up"), constraints: lambda { |request| Passenger.where(shortcut: request[:shortcut]).any? }

Controller action not executing

Environment:
Rails: 3.2.12
Ruby: ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux]
In my routes.rb, I have:
match '/attendance', to: "attendance#attendance_dashboard"
In my attendance_controller.rb, I have:
before_filter :authenticate_user!
before_filter :allow_only_if_admin
def attendance_dashboard
logger.info("File: #{__FILE__} -- Line: #{__LINE__}")
#registered_and_not_checked_in = get_registered_and_not_checked_in()
end
I am running on the test system, so logger.info should write to the test.log file.
When I run the /attendance action, I get 500 error, and here's what I see in the test.log file:
Started GET "/attendance" for 73.169.212.94 at 2015-02-06 15:11:06 +000
Processing by AttendanceController#attendance_dashboard as HTML
[1m[35mUser Load (0.4ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
Completed 500 Internal Server Error in 215ms
I was expecting to have the logger info requested logged to the test.log file, but it's not there, so I'm failing prior to that. Any ideas?
Solution:
The issue was not with the code, it was that user #2, who is signed in as an admin, has a JSON record in his DB record that was failing to unpack properly. Once that was fixed the issue went away.

Rails routes in production are looking for param: id. How do I tell it to use :order_id?

I recently connected a remote DB to the production version of my site. However, now my routes do not work.
In my Heroku logs, my app is looking for Parameters: {"id"=>"edit"}.
But there is not an :id parameter in my database. The only index specified is order_id.
How can I edit routes.db so that the order_id is used in my paths? And is this even necessary or is there something else I should look at?
List of columns in local db:
1.9.3-p392 :001 > Order.column_names
=> ["order_id", "product_status", "img_reference", "img_source", "user_email", "user_name", "user_address", "user_city", "user_zipcode", "user_state", "product_size", "product_type", "payment_id", "image_file_name", "image_content_type", "image_file_size", "image_updated_at"]
List of columns in remote db:
irb(main):001:0> Order.column_names
=> ["order_id", "order_date", "product_status", "img_reference", "img_source", "user_email", "user_name", "user_address", "user_city", "user_zipcode", "user_state", "product_size", "product_type", "uservendor_id", "paypal_confirmation"]
Again, my routes seem to work just fine in my development version.
The only difference I can think of is there's a few extra columns in my remote db which I don't think should effect the routes.
This is the error I get in my Heroku logs:
2014-05-08T15:28:55.461192+00:00 app[web.1]: Started GET "/orders//edit" for 76.102.227.168 at 2014-05-08 15:28:55 +0000
2014-05-08T15:28:55.465958+00:00 app[web.1]: Processing by OrdersController#show as HTML
2014-05-08T15:28:55.461180+00:00 app[web.1]: Started GET "/orders//edit" for 76.102.227.168 at 2014-05-08 15:28:55 +0000
2014-05-08T15:28:55.465965+00:00 app[web.1]: Processing by OrdersController#show as HTML
2014-05-08T15:28:55.465988+00:00 app[web.1]: Parameters: {"id"=>"edit"}
2014-05-08T15:28:55.466009+00:00 app[web.1]: Parameters: {"id"=>"edit"}
2014-05-08T15:28:55.469914+00:00 app[web.1]: Completed 404 Not Found in 4ms
2014-05-08T15:28:55.469922+00:00 app[web.1]: Completed 404 Not Found in 4ms
2014-05-08T15:28:55.470930+00:00 app[web.1]:
2014-05-08T15:28:55.470933+00:00 app[web.1]: ActiveRecord::RecordNotFound (Couldn't find Order without an ID):
2014-05-08T15:28:55.470935+00:00 app[web.1]: app/controllers/orders_controller.rb:69:in `set_order'
2014-05-08T15:28:55.470938+00:00 app[web.1]:
2014-05-08T15:28:55.470937+00:00 app[web.1]:
2014-05-08T15:28:55.470943+00:00 app[web.1]:
2014-05-08T15:28:55.470945+00:00 app[web.1]: ActiveRecord::RecordNotFound (Couldn't find Order without an ID):
2014-05-08T15:28:55.470946+00:00 app[web.1]: app/controllers/orders_controller.rb:69:in `set_order'
2014-05-08T15:28:55.470948+00:00 app[web.1]:
2014-05-08T15:28:55.470949+00:00 app[web.1]:
So, in ActiveRecord if you have a custom primary key you need to specify it in the model
class Order
self.primary_key = :order_id
Docs: http://guides.rubyonrails.org/active_record_basics.html#overriding-the-naming-conventions

Resources