When I run into an error on a rails 5.0 app page while on development mode, I receive an error page with the rails web-console at the bottom of the page like in the screenshot below. The console seems to be pretty useful for running the methods of that controller in which the error occurred.
Is it possible to enable it on all the pages so I can access it without an error page?
On the web-console gem page, it says you can manually run the console in any page of your application. The docs says the following:
For example, calling console in a view will display a console in the current page in the context of the view binding.
<% console %>
Calling console in a controller will result in a console in the context of the controller action:
class PostsController < ApplicationController
def new
console
#post = Post.new
end
end
The method is defined in Kernel and you can invoke it any application code.
Only one console invocation is allowed once per request. If you happen to have multiple ones, a WebConsole::DoubleRenderError will be raised.
Maybe this will help you call the console on the pages you want it to appear.
Related
In our app, we have Sidekiq and have modified the sidekiq.rb file to have it require authentication to view the page.
sidekiq.rb
Sidekiq::Web.use(Rack::Auth::Basic) do |user, password|
[user, password] == [ENV['SIDEKIQ_USER'], ENV['SIDEKIQ_PASSWORD']]
end
We want to have an error page set up for whenever the ENVs above are missing it will let us know specifically that the missing ENVs are the issue.
Solution 1: check the Sidekiq web code, open its base controller class and write a before action to render an error page when error happens.
Solution 2: write a conditional route, show the error page when error happens
Solution 3: throw errors in sidekiq.rb, so your server wont startup successfully, you will not need an error page anymore. I think the last one is the best, because this is just internal page to be viewed by few people, and probably only you, its better to find the problem as soon as possible.
I am studying Ruby On Rails and complete novice at it. I find that it is a bit difficult to trace the process order of a functionality (e.g.: user clicks a button in the form).
I wonder if there is any tool or way to know the process (ie: What method in a controller is called, corresponding model method,....). I would like to trace the order of regarding process and it helps me much in debugging.
You can use byebug or other debugging tools. Example and other ways of debugging can be found at rails website: http://edgeguides.rubyonrails.org/debugging_rails_applications.html
For example if you choose byebug and decide to put it in a controller:
class PeopleController < ApplicationController
def new
byebug
#person = Person.new
end
end
When your code reaches byebug line of your controller it will pause and wait for your command at your rails server command terminal. You can give various commands at this point,
for example,
if you type next it will move to next line.
if you type instance_variables to see all the variable values.
If you type thread list for list all threads and their statuses.
I have a very simple set up where I make an API call, which calls a function, which will initialize an instance of a class. Weirdly, it works the first time, but any additional attempts to refresh the page gives me an uninitialized constant error for the very class being initialized. Here's an example
Rails 3.1
Ruby 2.0
in app/controllers/static_pages_controller.rb
class StaticPagesController < ApplicationController
require_relative 'test.rb'
def about
build_fleet()
render text: "This worked"
end
end
and in my app/controllers/test.rb:
class Fleet
def initialize(side)
#ships = []
#passive_abilities = []
#side = side
end
end
def build_fleet()
att_fleet = ::Fleet.new("att")
def_fleet = ::Fleet.new("def")
end
I go to localhost/static_pages/about and get "This worked". Hit refresh and see "Fleet uninitialized" complete with the appropriate fleet stack.
When I check the server log I see
>Started GET "/static_pages/about" for 127.0.0.1 at 2014-04-05 15:52:39 -0700
> Processing by StaticPagesController#about as HTML
>Completed 500 Internal Server Error in 4ms
>
>NameError (uninitialized constant Fleet):
> app/controllers/test.rb:10:in `build_fleet'
> app/controllers/static_pages_controller.rb:4:in `about'
What's going wrong on the reload?
This seems related to how rails in development mode tries to automatically reload code on each request.
Try the advice in this answer and replace the call to require_relative with require_or_load "./test.rb"
*Edit: *
I think what's happening is that at the end of every request in development mode, rails undefines most constants it knows about. (Classes are constants.)
The next request comes in and you ask ruby to load the file. But since this second request is part of the same process, ruby remembers that it already loaded test.rb and so it is skipped.
However, it looks like Fleet is a model (even if not a database-backed model). I'd drop it in the app/models/fleet.rb and rails will auto load it just fine.
I have a view named new.html.erb with the following code:
<%= some_non_existent_thing.imaginary_method %>
Now, I only see one simple 500 error page, like that:
500 Internal Server Error
If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong.
Shouldn't I see a pretty formatted page with some information about the exception?
I'm not sure I miss something here, but I believe rails used to show the full error page in development environment when there is something wrong in the view.
Are you sure that you are running the development environment? Check that RAILS_ENV=development or that you are running rails server -e development.
Then check your development.rb file, you should have the following line in it
config.consider_all_requests_local = true
If you happen to have an exception inside an exception, Rails has a middleware that catches it and returns a FAILSAFE_RESPONSE with the following copy:
500 Internal Server Error
If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong.
A nice way to troubleshoot this is to compare your custom error code with the sample code provided in the Rails 4.2.0 guides.
I'm pointing to that particular version because that whole section was removed in the Rails 5.0.0 guides.
Ideally, you should keep your error views, layout, and controller as free of logic as possible, to avoid running into this issue.
Firstly, as Anton mentions in the answer below, confirm that your config/environments/development.rb has:
config.consider_all_requests_local = true
and that you are running the server in development mode.
I had ensured the above and still observed the error.
This happened in my case, because my logger had some errors. I was using a custom log formatter, which used the String#% method to format the log. % seems to be very buggy and leads to all these weird errors. I figured this one out by adding a debugger line to the controller method and stepping through into the implicit render function call. It hit the bug and reported it as a malformed format string error.
This was the log formatter I was using before, which caused the bugs [I had added it to an initializer file]:
class Logger::SimpleFormatter
def call(severity, time, progname, msg)
"%-7s #{msg}\n" % severity
end
end
These modifications fixed the bug:
class Logger::SimpleFormatter
def call(severity, time, progname, msg)
severity_prefix = "[#{severity}]".ljust(7)
"#{severity_prefix} #{msg}\n"
end
end
This happens if the code is not compilable, for example if you have an if statement missing an end.
I am trying to build a very simple rails application.
When user submits a form I process some files on the server and display the results.
Clearly, I do not need a database for this. However, I created a model so that I can put all my processing logic in it.
In in the controller, I call the process function in the model. for example
# action in controller
def my_action
MyModel.process(params)
end
However, when I run the server and submit the form, rails says MyModel is uninitialized
uninitialized constant MyController::MyModel
What am I doing wrong?
Where do you define MyModel? Is it in app/models/my_model.rb as Rails (to put it simply) expects it to be?