Rails ajax/partial not rendering properly - ajax

I'm trying to test using AJAX to list a downloaded item in rails. And everything up until the partial seems to work correctly. The only thing I can get it to return is and ID element, so download.id works or even download.someotherobject.id works (in a :belongs_to relationship, for example). But if I try to pull any of the other attributes (like the token attribute in my example below) from the download object, it just fails silently. I don't get any errors in the log or the javascript console.
My method for testing:
def download
#download = Download.find(15)
respond_to do |format|
format.js {#download}
end
end
My download.js.erb:
$('#start_download_link').after(<%= render(#download) %>);
My downloads/_download.html.erb
<%= download.token %>
Here's the log:
Started GET "/start/download" for 127.0.0.1 at 2011-12-09 21:09:22 -0500
21:09:22 web.1 | Processing by StartController#download as JS
21:09:22 web.1 | Download Load (1.2ms) SELECT "downloads".* FROM "downloads" WHERE "downloads"."id" = ? LIMIT 1 [["id", 15]]
21:09:22 web.1 | Rendered downloads/_download.html.erb (0.1ms)
21:09:22 web.1 | Rendered start/download.js.erb (22.3ms)
21:09:22 web.1 | Completed 200 OK in 119ms (Views: 30.8ms | ActiveRecord: 2.7ms)
Except, I get nothing in rendered in the view. But, again download.id does render the id. I can't figure out what's wrong.

If download.id is working but download.token is not, try checking if the field for if the token is set to null.

Related

Send logs to datadog with custom formatter shows only the first log of multiple ones

I am trying to send logs to DD (datadog) in such a way that the logs are being received as json and therefore shown properly in the portal through attributes.
My logger is a simple Logger.new(STDOUT, level: Logger::INFO).
If I stick to its standard output, it will in the form
I, [2022-07-30T22:43:35.216846 #1] INFO -- my-app: {"user":"1234"}
which is not really parsable by DD since not a proper JSON. In this case however all the logs appear at least on the DD portal.
Now.. I am trying to format the logs in a JSON manner in this way:
def self.logger
#logger ||= Logger.new(STDOUT, level: Logger::INFO)
#logger.progname = 'my-app'
#logger.formatter = proc do |severity, datetime, progname, msg|
{timestamp: datetime.to_s, progname: progname, severity: severity, correlation: Datadog::Tracing.log_correlation, message: msg}.to_json
end
#logger
end
This is my logger and thanks to this logs are seen properly in DD and parsed correctly because formatted in my app in a proper JSON.
The problem with this approach though seems to be that the logs are sent in 1 full block. Meaning that only the very first log is being visible. Let's say that I want to log this:
my_hash = {"message" => '1', "prop" => '1234'}.to_json
logger.info(my_hash)
my_hash = {"message" => '2', "prop" => '12345'}.to_json
logger.info(my_hash)
only the first log will be shown correctly on the DD portal. Parsed correctly with its message and prop attributes, but nothing about the second log.
Here is the thing, if I see the output of my app locally in the console I see this:
{"timestamp":"2022-07-31 01:15:39 +0200","progname":"my-app","severity":"INFO","correlation":"dd.service=my-app dd.trace_id=2976451780376429536 dd.span_id=0","message":"{"message":"1","prop":"1234"}"}{"timestamp":"2022-07-31 01:15:39 +0200","progname":"my-app","severity":"INFO","correlation":"dd.service=my-app dd.trace_id=2976451780376429536 dd.span_id=0","message":"{"message":"2","prop":"12345"}"}127.0.0.1 - - [31/Jul/2022:01:15:39 +0200] "GET /controller/test_controller HTTP/1.1" 200 - 0.0024
so the 2nd log gets actually outputted! But DD somehow sees only the first log..
(I know there is even a 3rd one shown in this message.. but that's just Sinatra automatic behavior for every http call reaching the api). What do you guys think is the problem?

Using Rack to run a React application is not working as it should

My idea is using Rack to load a web application separated in two parts. This is the structure:
my_app
|______api
| |______poros
|
|______fe
| |______build
| |______node_modules
| |______package.json
| |______public
| | |______ index.html
| | ...
| |______README.md
| |______src
| |______index.js
| ...
|______config.ru
The fe part (which stands for front-end) is a React application created with create-react-app. I've built it with
npm run build
and then its static optimized build is under my_app/fe/build directory.
This is my my_app/config.ru:
#\ -w -p 3000
require 'emeraldfw'
use Rack::Reloader, 0
use Rack::ContentLength
run EmeraldFW::Loader.new
And this is my EmeraldFW::Loader class, which is part of a gem installed and running fine.
module EmeraldFW
class Loader
def call(env)
path_info = (env['PATH_INFO'] == '/') ? '/index.html' : env['PATH_INFO']
extension = path_info.split('/').last.split('.').last
file = File.read("fe/build#{path_info}")
[200,{'Content-Type' => content_type[extension]},[file]]
end
def content_type
{
'html' => 'text/html',
'js' => 'text/javascript',
'css' => 'text/css',
'svg' => 'image/svg+xm',
'ico' => 'image/x-icon',
'map' => 'application/octet-stream'
}
end
end
end
As you may see, this is all quite simple. I capture the request with in my EmeraldFW::Loader class and transform its path_info it a bit. If the request is '/' I rewrite it to '/index.html' before doing anything else. In all cases I prepend fe/build to make it load from the static build of the React application.
When I run
rackup config.ru
and load the application at http://localhost:3000 the result is completely fine:
[2017-03-15 21:28:23] INFO WEBrick 1.3.1
[2017-03-15 21:28:23] INFO ruby 2.3.3 (2016-11-21) [x86_64-linux]
[2017-03-15 21:28:23] INFO WEBrick::HTTPServer#start: pid=11728 port=3000
::1 - - [15/Mar/2017:21:28:27 -0300] "GET / HTTP/1.1" 200 386 0.0088
::1 - - [15/Mar/2017:21:28:27 -0300] "GET /static/css/main.9a0fe4f1.css HTTP/1.1" 200 623 0.0105
::1 - - [15/Mar/2017:21:28:27 -0300] "GET /static/js/main.91328589.js HTTP/1.1" 200 153643 0.0086
::1 - - [15/Mar/2017:21:28:28 -0300] "GET /static/media/logo.5d5d9eef.svg HTTP/1.1" 200 2671 0.0036
::1 - - [15/Mar/2017:21:28:28 -0300] "GET /static/js/main.91328589.js.map HTTP/1.1" 200 1705922 0.1079
::1 - - [15/Mar/2017:21:28:28 -0300] "GET /static/css/main.9a0fe4f1.css.map HTTP/1.1" 200 105 0.0021
As you may see, all resources are loading correctly, with the correct mime types. But it happens that my default React logo, which should be spinning in my app frontpage, is not there! As if it wasn't loaded.
The big picture of all this is having this rack middleware loading the React front-end of the app and, at the same time, redirecting correctly the api requests made with fetch from the front-end to the poros (Plain Old Ruby Objects) which are the API part.
The concept is quite simple. But I just can't understand why this specific resource, the svg logo, is not loading.
It was all a matter of wrong mime type, caused by a typo.
It may be clearly seen in my question that I wrote:
def content_type
{
'html' => 'text/html',
'js' => 'text/javascript',
'css' => 'text/css',
'svg' => 'image/svg+xm', # <== Here is the error!!!
'ico' => 'image/x-icon',
'map' => 'application/octet-stream'
}
end
when the correct mime type for SVG files is 'image/svg-xml', with an 'l'...
This was making the browser ignore the file received and so it won't display it correctly.

heroku root :to => 'refinery/pages#home' not work

Start up page worked fine at local
http://localhost:3000
http://localhost:3000/shop
http://localhost:3000/refinery/pages#home
http://www.myweb.com/ not work
In Gemfile:
root :to => 'refinery/pages#home'
mount Spree::Core::Engine, :at => '/shop'
mount Refinery::Core::Engine, at: Refinery::Core.mounted_path
Here what happened:
http://www.myweb.com/shop =========> Work
http://www.myweb.com/pages/home ===> Work
http://www.myweb.com ==============> The page you were looking for doesn't exist.
Problem here: http://www.myweb.com not work
Don't know why. Could anyone fix this
Thanks
Here what I found in heroku after pushed:
Problem:
In logs refinery searched for record with ["link_url", "/" but none
that is the error in data not the routes.rb
So I should assumed this is a bug of refinery
2016-11-19T23:47:41.062659+00:00 app[web.1]: Refinery::Page Load (2.3ms) SELECT "refinery_pages".* FROM "refinery_pages" WHERE "refinery_pages"."link_url" = $1 LIMIT 1 [["link_url", "/"]]
2016-11-19T23:47:41.077929+00:00 app[web.1]: Refinery::Page Load (1.4ms) SELECT "refinery_pages".* FROM "refinery_pages" WHERE "refinery_pages"."menu_match" = $1 ORDER BY "refinery_pages"."id" ASC LIMIT 1 [["menu_match", "^/404$"]]
2016-11-19T23:47:41.118752+00:00 app[web.1]: Rendered public/404.html (1.5ms)
Quick Solution:
Edit the record column "link_url" with "/"

Mongoid Delete Document matching ID

I'm trying to remove a duplicate document in my collection. Here's my code:
class Sites
include Mongoid::Document
store_in collection: "sites"
end
Sites.destroy_all(conditions: { _id: BSON::ObjectId("5685a45be4b06ab5911dcd12")})
Here's what is returned:
D, [2015-12-31T17:39:16.488657 #10126] DEBUG -- : MONGODB | localhost:27017 | rails.find | STARTED | {"find"=>"sites", "filter"=>{"conditions"=>{"_id"=>BSON::ObjectId('5685a45be4b06ab5911dcd12')}}}
D, [2015-12-31T17:39:16.488946 #10126] DEBUG -- : MONGODB | localhost:27017 | rails.find | SUCCEEDED | 0.000189083s
But when I search the document is still there :(
{"_id"=>BSON::ObjectId('5685a45be4b06ab5911dcd12'), "name"=>"StackOverflow", "title"=>"Stack Overflow", "type"=>"Forum", "url"=>"http://www.stackoverflow.com"}
How do I permanently delete a document by reference using Mongoid?
Mongoid with 5.x follows a lot more closely to the AR methods we are used to so your id field is behaves in a similar way.
Sites.where(id: '5685a45be4b06ab5911dcd12').delete
To have all the necessary callbacks be called as well just use .destroy. Just know this loads everything into memory and can be expensive.
Sites.where(id: '5685a45be4b06ab5911dcd12').destroy
After doing a lot of iterations this works for me:
Sites.destroy_all({ :_id => BSON::ObjectId('5685a45be4b06ab5911dcd12')})
I've found that Mongoid is not very helpful when troubleshooting. I have to do a lot more trial and error to discover how to do things :(

Redirecting to #timecard results in 'no route matches [PATCH] "/timecards"

Can't figure out this routing problem: I redirect from my update controller action to the modified #timecard, and get the following error: No route matches [PATCH] "/timecards"
Why does rails try to redirect using PATCH and to timecards#index?
Is that not how I redirect to the recently edited #timecard?
In timecards_controller.rb:
def edit
#timecard = Timecard.find(params[:id])
end
def update
#timecard = Timecard.find(params[:id])
#timecard.update_attributes!(timecard_params)
redirect_to #timecard
end
Output of rake routes:
$ rake routes
Prefix Verb URI Pattern Controller#Action
timecard_timecard_entries GET /timecards/:timecard_id/timecard_entries(.:format) timecard_entries#index
POST /timecards/:timecard_id/timecard_entries(.:format) timecard_entries#create
new_timecard_timecard_entry GET /timecards/:timecard_id/timecard_entries/new(.:format) timecard_entries#new
edit_timecard_timecard_entry GET /timecards/:timecard_id/timecard_entries/:id/edit(.:format) timecard_entries#edit
timecard_timecard_entry GET /timecards/:timecard_id/timecard_entries/:id(.:format) timecard_entries#show
PATCH /timecards/:timecard_id/timecard_entries/:id(.:format) timecard_entries#update
PUT /timecards/:timecard_id/timecard_entries/:id(.:format) timecard_entries#update
DELETE /timecards/:timecard_id/timecard_entries/:id(.:format) timecard_entries#destroy
timecards GET /timecards(.:format) timecards#index
POST /timecards(.:format) timecards#create
new_timecard GET /timecards/new(.:format) timecards#new
edit_timecard GET /timecards/:id/edit(.:format) timecards#edit
timecard GET /timecards/:id(.:format) timecards#show
PATCH /timecards/:id(.:format) timecards#update
PUT /timecards/:id(.:format) timecards#update
DELETE /timecards/:id(.:format) timecards#destroy
root GET / timecards#index
When you call redirect_to you should specify a path.. not necessarily the object itself.
Try,
def update
#timecard = Timecard.find(params[:id])
#timecard.update_attributes!(timecard_params)
redirect_to timecard_path(#timecard)
end
The problem was naturally in the view. Thanks to #sircapsalot for pointing me to the log, which indicated that the incorrect route was indeed being called (from an incorrect link in the view).

Resources