This might be (read: probably is) a dumb question, but here goes...
Is there a simple, preferably non third-party, way to display Rails logs in the browser? It gets kind of old opening a log file, then closing it and re-opening it on the next error. It would be superfantastic to just to go domain.tld/logs, which would require user authentication, of course.
For reference, there is a very simple way to do this. However, you would want to protect this page with some sort of authentication/authorization.
Controller:
lines = params[:lines]
if Rails.env == "production"
#logs = `tail -n #{lines} log/production.log`
else
#logs = `tail -n #{lines} log/development.log`
end
Log File View:
<pre><%= #logs %></pre>
View to display link:
<%= link_to "log file", log_path(lines: 100) %>
Routes:
get 'logs/:lines' => "pages#log", as: "log"
All you need is to open log file and put its content into browser.
Realted topic: ruby: all ways to read from file.
Also you should know that your logs grow very fast, and it's not a good idea to show whole log in browser. You can just open last 100-200 rows. Related topic: Reading the last n lines of a file in Ruby?
Also you can try this solution: http://newrelic.com/. It is more complex and little oftopic, but quite useful.
I created a for this purpose called browserlog.
Installing is just a matter of adding the gem to the Gemfile:
gem 'browserlog'
Afterwards all you need is to mount the engine on the route you want:
MyApp::Application.routes.draw do
mount Browserlog::Engine => '/logs'
end
Once that's set up, accessing /logs/development (or production, test, etc) will show a window like this:
(source: andredieb.com)
Pimp my Log can be used to visualization many types of logs including Ruby on Rails.
It also includes the management of users and notification.
[Edited]
By default, the PimpMyLog does not support Ruby on Rails logs. It's necessary to implement the regex to works with it.
[/Edited]
Related
We've just inhered a code base from a new client and are in the process of bringing it across to our systems.
We've got a small problem with the rake db:schema:load command, in that it's failing due to the following lines in the routes.rb file:
if Rails.env.development? || Rails.env.staging?
Country.all.each do |country|
get "/#{country.locale}", to: 'home_pages#index', locale: country.locale
end
end
As you can see, this is checking if we're in the development environment, then attempting to dynamically generate routes for each of the countries in the database - but of course, this is failing because there are no countries in the database.
A classic catch 22 ;)
What I was hoping to find out is if there some way I can avoid this from happening? I can temporarily comment out the offending lines from the routes.rb file, but this feels a bit like cheating, and figured there must be a way of doing this which was a bit more elegant.
Thank you.
I can see 2 possible solutions:
1 define a dynamic route with a constraint
get '/:locale', to: 'home_pages#index', constraint: -> { |req| Country.where(locale: req.parameters[:locale]).exists? }
Note: you don't need locale: country_locale anymore, because :locale (in the route declaration) will give you this param available in the request object.
What the constraint lambda trick does is - it checks the validity of the requested route after the app is loaded (so Companies does not have to be in the DB when the routes file is loaded and evaluated).
The downside is the extra hit to the DB each time there's a request matching that route. Maybe you can mitigate it with some caching.
The upside is that you can now add new countries with new locales at runtime (no need to restart the app), but I guess this is not a major concern.
2 dump locales to a file and keep it in sync with the DB
You can keep the way you create routes (iterate through all locales and define one route for each), but load the locales from the file.
File.open('locales.txt').each_line do |locale|
get "/#{locale}", to: 'home_pages#index', locale: locale
end
The downside is that you need to keep that in sync (for the occasion when some new country appears/disappears?) You can occasionally dump Company.pluck(:locale).join("\n") into this file.
I have a feeling that this particular rake task should not need routes defined, but probably it's the way the rails app environment is loaded. I had the same issue (we used solution 1 with cache BTW) and I think I reported it somewhere but I can't find it... will share when I succeed.
An answer which my coworker found was to do a check via ActiveRecord::Base.connection to see if the database table existed before running the code.
It now looks like this:
if (Rails.env.development? || Rails.env.staging? || Rails.env.uat?) && ActiveRecord::Base.connection.table_exists?('countries')
Country.all.each do |country|
get "/#{country.locale}", to: 'home_pages#index', locale: country.locale
end
end
If I'm testing my Javascript code, I often use console.log to write messages to the browser console. I find it a convenient place to check for these messages.
Is it possible to output messages to the browser console from Ruby files within your Rails project? (E.g. from a method in the model)
You can use console.log() in your views:
#view.html.erb
<script>
console.log("Message");
</script>
If you want to log in your Model, try:
Rails.logger.debug "message!!"
Simple. Just call puts 'your debug message' and it will be printed to where the server is logging. For instance, if you are running the rails server only by running rails s on a terminal, the output of puts will be on this same terminal. If you need more 'power' to debug, you should consider using IDE's like RubyMine to debug your code. Thus, you can place breakpoints and see all the application state.
Not really, as cstrutton mentioned printing to the browser log is not possible. But this is something I tend to do which may help others in the same situation.
Say for example, I only want to display my debug params on my page if my :commit == "Search". I would do something like this:
In my helper file.
def view_logging
if params[:commit] == "Search"
debug params
else
# add whatever you want here if not true
end
end
Now inside my view page, I would just simply add the helper method defined.
<%= view_logging %>
I'm building a API and want the show page for a user to be found by 'uid' instead of the record ID
I have this in my controller
def show
respond_with User.find_by_uid(params[:uid])
end
When I go to localhost/api/v1/users/8888888 Its returns "Null"
Finding by ID seems to work fine, am I doing something wrong here?
I tried put this in the rails console and it worked
User.find_by_uid("8888888")
I'm new to rails
Thanks
have you tried visiting:
localhost/api/v1/users?uid= 8888888 instead of the url you are using currently, except you are handling that correctly rails would have no knowledge of the uid param alternatively you could add this to your config/routes.rb file
get 'users/:uid', to: 'users#show'
With the hope that your controller is called UsersController then you can call localhost/api/v1/users/8888888 in your browser and it should behave as expected
Rather than just giving you the answer, I'll provide a tip on debugging Ruby applications (including Rails).
Get the pry gem and the pry-debugger gem and include them in your Rails application (there's plenty of posts around Google on how to include pry and pry-debugger in Rails).
put 'binding.pry' (without the quotes) at the beginning of your show method. In the console where your server runs, when show gets executed, execution will halt/pause at binding.pry. Type the following in the pry console to see what is available in the rails params hash.
pry> params
(this will print out the contents of params)
I would start my troubleshooting here, and post the contents of params and any relevant server logging here if you still can't figure it out.
edit
I don't have enough rep to comment, yet. Only really been on this site and using it a day or two.
I have a bit of code that I need to run when a user loads a page. What it does specifically is update my database based on an xml file using a gem. So far I've found answers that tell me I should put it in everything from a rake task to the lib folder itself to the model. It's all a little confusing.
Here's the code in question:
require 'rubygems'
require 'eaal'
EAAL.cache = EAAL::Cache::FileCache.new
api = EAAL::API.new("id", "vcode", "char")
result = api.MarketOrders("characterID" => "id")
result.orders.each do |order|
#found = MarketItem.find_by_typeid(order.typeID.to_i)
MarketItem.update(#found.id, :remaining => order.volRemaining.to_i)
end
I'm sorry if this is an obvious question and I'm sure my code is horrible. I'm really new to rails and the only way I seem to be able to learn new languages is the bull-in-a-china-shop method.
There is a route to that page that hits a function first
for example. show_user_path will hit the function show. You can put your code at the beginning of the function that renders the page.
I'm find such a gem a whole day, but not find a good one. I want to write one, but I'm not able to do it.
The data in my database may be English text, which will be dump to a yml file with plain text. And some are non-English text, which will be binary type.
And both of them may have such code:
<% xxx %>
When I use rake db:fixtures:load to load them into database, error may occur: method xxx not found.
I wan't to find a good gem can handle this problem. Thank for any help
UPDATE
I have gave up finding such a gem. At first, I think it's a easy task, but now, after some researchings, I have to say, it's much harder than I expected.
The reason you are having problems is because the Fixture loader will pass your fixture through erb before loading the data. This means that if you have <% xxx %> in your yaml file then Rails will see that as erb and try to run a method called xxx.
There does not seem to be an easy way to turn off erb processing of fixtures. I have tried replacing the fixtures with CSV fixtures and this still does ERB pre-processing.
Without a simple answer I have to ask the question Why do you have these strings in your file?
Do you want them to be expanded by erb?
Err...I'm not sure if you actually need a gem for this? Rails natively can turn any model into YAML.
Let's say you have a model called "Objects". You could hit a route that looks like:
/objects.yaml
and you would get a giant text file of all your Objects in YAML form.
Of course, you would want to have something like:
respond_to do |format|
format.yaml {render :yaml => #objects}
end
in your restful controller.
If you'd rather not hit a route to do this, you can always do
#yaml = []
#objects.each do |object|
#yaml.push object.to_yaml
end
anywhere in ruby, which will give you an array of yaml objects, that you can then write to a file at your leisure.
I imagine that if rails itself is generating the yaml, then it would be able to then later load it as a fixture?