Rails calling deleted Controller - ruby-on-rails

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

Related

Rails Receives Repetitive Requests on One Request

My route is as follows:
Rails.application.routes.draw do
get 'courses' => 'application#index'
get 'check_db' => 'application#check_db'
root 'application#index'
end
And my application#check_db is
def check_db
p "test"
redirect_to root_path
end
When I visit /check_db on my browswer (tried both in Chrome and Safari), if working properly, it should GET "/check_db" and then GET "/". But actually, the log shows that there are four requests, with the above pattern repeated twice (i.e. GET "/check_db", GET "/", GET "/check_db", GET "/"). There is absolutely no code in my application#index that does any other redirection. So why is this?
The situation described above happen most of the times. However, occasionally, it works as expected. I did not change the code in the interim.
If it is in interest, the entire log is pasted below:
Started GET "/check_db" for ::1 at 2017-01-04 17:05:06 -0800
Processing by ApplicationController#check_db as HTML
"test"
Redirected to http://localhost:3000/
Completed 302 Found in 1ms
Started GET "/" for ::1 at 2017-01-04 17:05:06 -0800
Processing by ApplicationController#index as HTML
Rendering application/index.html.erb within layouts/application
[MongoDB query log]
Rendered application/index.html.erb within layouts/application (3.4ms)
Completed 200 OK in 18ms (Views: 16.4ms)
Started GET "/check_db" for ::1 at 2017-01-04 17:05:07 -0800
Processing by ApplicationController#check_db as HTML
"test"
Redirected to http://localhost:3000/
Completed 302 Found in 8ms
Started GET "/" for ::1 at 2017-01-04 17:05:07 -0800
Processing by ApplicationController#index as HTML
Rendering application/index.html.erb within layouts/application
[MongoDB query log]
Rendered application/index.html.erb within layouts/application (4.1ms)
Completed 200 OK in 17ms (Views: 15.0ms)

Rails 4: Generate Scaffold Controller with existed Model, it only works after reloading page twice

I generate Model first
rails g model cat name
Then I genarate Scaffold Controller with the model name
rails g scaffold_controller zoo/cat name --model-name=zoo/cat
Then I add route
namespace :zoo do
resources :cats
end
Then I edit the Model
class Zoo::Cat < ActiveRecord::Base
end
The funny thing is it fails at first, with error
LoadError in Zoo::CatsController#index
Unable to autoload constant Cat, expected /home/minh/Desktop/fnl110/app/models/cat.rb to define it
def index
#zoo_cats = Zoo::Cat.all
end
Then I reload the page, it works without any error. Here is what server said:
Started GET "/zoo/cats" for 127.0.0.1 at 2016-01-10 21:05:30 +0700
ActiveRecord::SchemaMigration Load (0.1ms) SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by Zoo::CatsController#index as HTML
Completed 500 Internal Server Error in 14ms (ActiveRecord: 0.0ms)
LoadError (Unable to autoload constant Cat, expected /home/minh/Desktop/fnl110/app/models/cat.rb to define it):
app/controllers/zoo/cats_controller.rb:7:in `index'
Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_source.erb (7.1ms)
Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.2ms)
Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (29.2ms)
Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.5ms)
Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.4ms)
Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms)
Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.4ms)
Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (19.6ms)
Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.5ms)
Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.6ms)
Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (42.4ms)
Started GET "/zoo/cats" for 127.0.0.1 at 2016-01-10 21:10:15 +0700
Processing by Zoo::CatsController#index as HTML
Zoo::Cat Load (0.2ms) SELECT `cats`.* FROM `cats`
Rendered zoo/cats/index.html.erb within layouts/application (8.6ms)
Completed 200 OK in 200ms (Views: 197.5ms | ActiveRecord: 1.2ms)
Started GET "/assets/animal/bears.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700
Started GET "/assets/animal/cats.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700
Started GET "/assets/animal/dogs.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700
Started GET "/assets/scaffolds.self-d5c911709e0018272624ff2abf5748b5d0ff72c54f791bb9bcefd817c3322285.css?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700
Started GET "/assets/turbolinks.self-c37727e9bd6b2735da5c311aa83fead54ed0be6cc8bd9a65309e9c5abe2cbfff.js?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700
Started GET "/assets/jquery_ujs.self-d456baa54c1fa6be2ec3711f0a72ddf7a5b2f34a6b4f515f33767d6207b7d4b3.js?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700
Started GET "/assets/application.self-e80e8f2318043e8af94dddc2adad5a4f09739a8ebb323b3ab31cd71d45fd9113.css?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700
Started GET "/assets/animal/bears.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700
Started GET "/assets/jquery.self-a714331225dda820228db323939889f149aec0127aeb06255646b616ba1ca419.js?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700
Started GET "/assets/animal/dogs.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700
Started GET "/assets/application.self-3b8dabdc891efe46b9a144b400ad69e37d7e5876bdc39dee783419a69d7ca819.js?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700
Started GET "/assets/animal/cats.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700
P/S: The reason of
rails g scaffold_controller zoo/cat name --model-name=zoo/cat
is
rails g scaffold_controller zoo/cat --model-name=zoo/cat
=> controller doesn't take model attributes
and
rails g scaffold_controller zoo/cat --model-name=cat
=> Error: undefined local variable or method `new_cat_path'
The error is normal, your model should be moved in the zoo directory, so app/models/zoo/cat.rb (instead of app/models/cat.rb). If you do that it will work immediately.
I might be wrong, but I believe the following happens:
When you load the page the first time, rails will lazily load the models, and will give you the error not because of the request, but because the file app/models/cat.rb should define Cat and not Zoo::Cat;
Even with the error, rails will have loaded the Zoo::Cat model, so when you reload the page, it won't encounter the error because the models have already been loaded, thus the request succeeds.

Rails route skips action and directly renders view

I have a namespaced controller (Interviews)
namespace :recruitment do
resources :interviews
end
I created a route (outside of the namespace)
get "myinterviews", to: "recruitment/interviews#my", as: :myinterviews
SO that /myinterviews shows the user his own interviews
Whereas rails seems to find the right view, it totally skips the controller action
def my
puts "Hi there"
exit
#recruitment_interviews=current_user.interviews
puts #recruitment_interviews.inspect
end
This is ofcourse inside Recruitments::InterviewController.
The view(recruitment/interviews#my) is rendered (it results in an error since recruitment_interviews is not found
Here's the process log:
Started GET "/myinterviews" for 127.0.0.1 at 2015-07-03 18:54:48 +0530
Processing by Recruitment::InterviewsController#my as HTML
Rendered recruitment/interviews/my.html.slim within layouts/application (3.7ms)
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
Rendered layouts/_navbar.html.slim (2.4ms)
Completed 200 OK in 199ms (Views: 197.6ms | ActiveRecord: 0.2ms)

authenticate_user! hijacking the registrations/create method

The problem I'm having seems to be that Devise's authenticate_#{role}! method is hijacking my registration attempt.
Started GET "/client/sign_up" for 127.0.0.1 at 2012-01-14 12:02:52 +0000
Processing by Client::RegistrationsController#new as HTML
Rendered /Users/robertwwhite/.rvm/gems/ruby-1.9.2-p290/gems/devise-1.5.3/app/views/devise/shared/_links.erb (1.4ms)
Rendered client/registrations/new.html.haml within layouts/application (97.6ms)
Rendered client/_navigation.html.haml (1.6ms)
Rendered shared/_flash_messages.html.haml (0.1ms)
Completed 200 OK in 126ms (Views: 116.4ms | ActiveRecord: 7.2ms)
Started POST "/client" for 127.0.0.1 at 2012-01-14 12:02:58 +0000
Processing by WishesController#index as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"vq3wgsQeb4eoxhb3sw2Q2kd4edIoOxIfrzJ/WzJUAn0=", "client"=>{"email"=>"bacon#example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Completed 401 Unauthorized in 13ms
Started GET "/client/sign_in" for 127.0.0.1 at 2012-01-14 12:02:58 +0000
Processing by Client::SessionsController#new as HTML
Rendered /Users/robertwwhite/.rvm/gems/ruby-1.9.2-p290/gems/devise-1.5.3/app/views/devise/shared/_links.erb (1.0ms)
Rendered client/sessions/new.html.haml within layouts/application (16.5ms)
Rendered client/_navigation.html.haml (1.5ms)
Rendered shared/_flash_messages.html.haml (0.3ms)
Completed 200 OK in 60ms (Views: 38.6ms | ActiveRecord: 6.4ms)
I've tried overriding the after_signup_path_for(resource_or_scope) but it seems to be getting ignored.
# app/controllers/application_controller.rb
def after_sign_up_path_for(resource_or_scope)
random_path
end
So as it stands users can't register to the site in the first place. Any ideas?
Have you checked to make sure non of your routes are overriding the default devise routes/methods?
Edited by HaaR for clarity of users with similar problem:
I had the following in my config/routes.rb above my devise_for methods.
match "client" => "wishes#index"
Which was overriding Devise's
devise_for :clients, :path => :client
By moving it below, it gives Devise priority, and still passes the get request to the appropriate controller and action without hijacking the POST requests.

How can I find the new session_id after the reset_session command in Rails 3?

I am using the command "reset_session" as per here to avoid Session Fixation.
After running the command I would like to store the new generated session_id in the database but session[:session_id] is not defined.
I wrote a simple test:
class ApplicationController < ActionController::Base
protect_from_forgery
after_filter :after_test
def after_test
RAILS_DEFAULT_LOGGER.debug "Old Session: #{session.inspect}"
reset_session
session[:random_number] = ((rand*1000).to_i)
RAILS_DEFAULT_LOGGER.debug "New Session: #{session.inspect}"
end
end
Result in the log for two conescutives pages load is:
Started GET "/" for 127.0.0.1 at 2011-04-16 11:42:57 +0200
Processing by WelcomeController#index as HTML
Rendered welcome/index.html.erb within layouts/application (1.9ms)
Old Session: {"random_number"=>519, "session_id"=>"d17df62e286f20bd25e2714ee4f58020", "_csrf_token"=>"NkD5ZjG/RYLolfRy0ADmr+h+Sp2TXEOQlc6HhNpyp/g="}
New Session: {:random_number=>172}
Completed 200 OK in 7ms (Views: 6.4ms | ActiveRecord: 0.0ms)
and
Started GET "/" for 127.0.0.1 at 2011-04-16 11:42:58 +0200
Processing by WelcomeController#index as HTML
Rendered welcome/index.html.erb within layouts/application (2.0ms)
Old Session: {"random_number"=>172, "session_id"=>"54f46f520c80044a9f5475af78a05502", "_csrf_token"=>"9skbBEN35jQYRgH9oQVz1D5Hsi/o9l7fm7Qx9XDNETc="}
New Session: {:random_number=>497}
Completed 200 OK in 7ms (Views: 6.4ms | ActiveRecord: 0.0ms)
As you can see the random number (172) is properly passed to the second page but the New Session does not show the new session id.
I think the new session id ("54f46f520c80044a9f5475af78a05502") is generated after the "after_filter" but I don't know how to retrieve it.
Yes, rails has this problem. There is a ticket - https://rails.lighthouseapp.com/projects/8994/tickets/2200-session-support-broken
And as you can see it hasn't been resolved yet.

Resources