Why is my rails app showing a blank page? - ruby-on-rails

I am going though Michael Hartl's rails Tutorial chapter 10 section 10.2. And my rails app is now showing a blank page for all pages I try to load on the local host. I have tried resetting the computer as suggested in another post, but this didn't work. I have checked the development log and here is the error message.
Started GET "/users/3" for 95.10.6.124 at 2017-11-05 16:36:43 +0000
Cannot render console from 95.10.6.124! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by UsersController#show as HTML
Parameters: {"id"=>"3"}
[1m[36mUser Load (0.2ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?[0m [["id", 3], ["LIMIT", 1]]
Rendering users/show.html.erb within layouts/application
Rendered users/show.html.erb within layouts/application (1.7ms)
Rendered layouts/_rails_default.html.erb (27.1ms)
Rendered layouts/_shim.html.erb (0.5ms)
[1m[36mUser Load (0.2ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?[0m [["id", 3], ["LIMIT", 1]]
Rendered layouts/_header.html.erb (2.3ms)
Rendered layouts/_footer.html.erb (0.5ms)
Completed 200 OK in 41ms (Views: 38.0ms | ActiveRecord: 0.4ms)
This is strange as I undid the changes I made and now it doesnt work. In fact the serve doesnt seem to start. Instead of seeing the usual "ctrl+c to stop" at the end, the command prompt appears. This hasnt happened before and to my mnowledge I havent done anything differently.
Could someone help me with this, thanks.

Related

Every time I add an image_tag to a page, the index is being rendered in the background (Rails 5)

I have a website with a large codebase.
I noticed that every page that is rendered and have a picture on it, after rendering the page, it makes another request to the index and loading everything in the background.
I have no idea why this happens.
Started GET "/ganenet/main" for ::1 at 2022-08-11 12:41:13 +0300
Processing by OwnersController#main as HTML
GanOwner Load (0.7ms) SELECT "gan_owners".* FROM "gan_owners" WHERE "gan_owners"."id" = $1 ORDER BY "gan_owners"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendering owners/main.haml within layouts/application
Gan Load (0.4ms) SELECT "gans".* FROM "gans" WHERE "gans"."gan_owner_id" = $1 [["gan_owner_id", 1]]
Rendered owners/main.haml within layouts/application (6.4ms)
Rendered layouts/_google_tag_manager.haml (1.4ms)
Rendered layouts/_mixpanel.haml (2.2ms)
Rendered layouts/_hotjar.haml (1.9ms)
Rendered shared/_intercom.haml (2.5ms)
Rendered shared/_onesignal.html (0.3ms)
Rendered shared/_staging_env.haml (2.4ms)
Rendered shared/email_subscriptions/_consultant.html (0.4ms)
Rendered shared/modals/_consultant.haml (3.1ms)
Rendered shared/_modal.haml (8.5ms)
Rendered shared/_header.haml (18.4ms)
Rendered shared/owners/_navbar.haml (7.3ms)
Rendered shared/_gtm.haml (2.2ms)
Rendered shared/_modal.haml (11.5ms)
Rendered shared/_modal.haml (4.2ms)
Rendered shared/_modal.haml (57.8ms)
Rendered shared/modals/_ganenet_lekan.haml (83.3ms)
Rendered shared/_modal.haml (0.9ms)
Rendered shared/modals/_thankyou_contact_soon.haml (3.4ms)
Rendered shared/_modal.haml (0.5ms)
Rendered shared/modals/_thankyou_generic.haml (3.0ms)
Rendered shared/modals/_video_modal.haml (3.8ms)
Rendered shared/_footer.haml (3.0ms)
Rendered shared/_accessibility.html (0.2ms)
Completed 200 OK in 254ms (Views: 246.9ms | ActiveRecord: 1.1ms)
This is when the page I want is loaded, which is fine. But right after this, I get more requests which come out of nowhere.
Started GET "/dist/instantsearch.min.js.map" for ::1 at 2022-08-11 12:41:13 +0300
Started GET "/" for ::1 at 2022-08-11 12:41:13 +0300
Processing by HomeController#index as HTML
Started GET "/" for ::1 at 2022-08-11 12:41:13 +0300
Processing by HomeController#index as HTML
And it loads a bunch of stuff from the database which are related to the homepage (which is HomeController#index.
I noticed that the index loads only on pages that have at least one image on it.
I couldn't find any info on this issue so far.
Would be happy to hear from experienced folks with this issue.

How do i fix the error bad URI(is not URI?): nil

Running
Rails 6.0.2.1
Ruby 2.6.5
I'm implementing photo upload using ActiveStorage and DropZoneJS but at this point, it throws an error on this particular page /users/2.
Better errors shows this
URI::InvalidURIError at /users/2
bad URI(is not URI?): nil
car model
def cover_photo(size_x, size_y)
if self.photos.length > 0
self.photos[0].variant(resize_to_limit: [size_x, size_y]).processed.service_url
else
"blank.jpg"
end
end
end
My log file
Started GET "/users/2" for ::1 at 2020-05-08 11:39:09 +0000
Processing by UsersController#show as HTML
Parameters: {"id"=>"2"}
[1m[36mUser Load (0.1ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?[0m [["id", 2], ["LIMIT", 1]]
↳ app/controllers/users_controller.rb:3:in `show'
Rendering users/show.html.erb within layouts/application
[1m[36mCar Load (0.1ms)[0m [1m[34mSELECT "cars".* FROM "cars" WHERE "cars"."user_id" = ?[0m [["user_id", 2]]
↳ app/views/users/show.html.erb:29
[1m[36mActiveStorage::Attachment Load (0.1ms)[0m [1m[34mSELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ?[0m [["record_id", 1], ["record_type", "Car"], ["name", "photos"]]
↳ app/models/car.rb:14:in `cover_photo'
[1m[36mActiveStorage::Blob Load (0.1ms)[0m [1m[34mSELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ? LIMIT ?[0m [["id", 26], ["LIMIT", 1]]
↳ app/models/car.rb:15:in `cover_photo'
[36m Disk Storage (0.0ms) [0m[34mChecked if file exists at key: variants/4u0kx27vmugn57zwn96o2t27mr71/36e628c6ec62cc8383a3ee5c0c8433e912780efead13846813a9f63693dd17eb (yes)[0m
[36m Disk Storage (0.5ms) [0m[34mGenerated URL for file at key: variants/4u0kx27vmugn57zwn96o2t27mr71/36e628c6ec62cc8383a3ee5c0c8433e912780efead13846813a9f63693dd17eb ()[0m
Rendered users/show.html.erb within layouts/application (Duration: 18.4ms | Allocations: 4342)
Completed 500 Internal Server Error in 21ms (ActiveRecord: 0.4ms | Allocations: 5357)
URI::InvalidURIError - bad URI(is not URI?): nil:
app/models/car.rb:15:in `cover_photo'
app/views/users/show.html.erb:36
app/views/users/show.html.erb:32
Started POST "/__better_errors/f90fad0cb966afba/variables" for ::1 at 2020-05-08 11:39:09 +0000
How do i fix this? And what is wrong with cover_photo?
Depending on your setup, I had fixed this issue by defining ActiveStorage::Current.host = "http://localhost:3000" right before the use of variant
https://github.com/rails/rails/issues/40855
Okay, i removed .service_url from
self.photos[0].variant(resize_to_limit: [size_x, size_y]).processed.service_url
So it became
self.photos[0].variant(resize_to_limit: [size_x, size_y]).processed
And now the error is gone and the page loads perfectly. Not sure why
Btw, can anyone explain?
I was having the same problem in the rails console, the solution was run this once:
ActiveStorage::Current.host = 'http://localhost:3000'
I got this information from #Jan's answer, and from here.

Why does my Capybara feature test intermittently fail when waiting for Ajax?

I'm using Capybara 2.13.0 and I've got a test that relies on an Ajax request adding dynamic content into the DOM.
test 'contact data becomes visible when providing the correct password' do
login_as_researcher
visit '/participants/3'
assert page.has_css?("ul#redacted-contact-data")
click_button('Unlock Contact Data')
# Use the correct password
fill_in('enter your password', with: 'password1')
# Ajax happens here. It renders a partial and places it in the dom.
click_button('Submit')
# Test sometimes fails here..
assert page.has_css?("ul#contact-data"), -> { page.body }
end
I've read a lot of articles that say Capybara automatically waits for elements to appear in the HTML and there's no need to manually tell Capybara to wait anymore, but I'm not experiencing this in my case.
75% of the time this test passes and when it does fail and the page is dumped, it contains the content of the partial rendered in the JS response instead of the full HTML page.
Any idea what's going on here? I can only assume it's due to how long it takes the server to respond the Ajax request each time and Capybara not waiting long enough in certain situations.
I've set Capybara.default_max_wait_time = 5 in my test_helper.
I've tried with both the selenium and poltergeist drivers and it makes no difference.
Here's the output from test.log
Started GET "/" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by HomeController#index as HTML
Researcher Load (0.3ms) SELECT "researchers".* FROM "researchers" WHERE "researchers"."id" IS NULL LIMIT 1
Redirected to http://www.example.com/session/new
Filter chain halted as :researcher_only rendered or redirected
Completed 302 Found in 30ms (ActiveRecord: 0.3ms)
Started GET "/session/new" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by SessionsController#new as HTML
Rendered sessions/new.html.erb within layouts/application (9.5ms)
Researcher Load (0.1ms) SELECT "researchers".* FROM "researchers" WHERE "researchers"."id" IS NULL LIMIT 1
Completed 200 OK in 248ms (Views: 247.6ms | ActiveRecord: 0.1ms)
Started POST "/session" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "username"=>"nick1", "password"=>"[FILTERED]", "commit"=>"Enter"}
Researcher Load (0.1ms) SELECT "researchers".* FROM "researchers" WHERE "researchers"."username" = ? LIMIT 1 [["username", "nick1"]]
Redirected to http://www.example.com/
Completed 302 Found in 78ms (ActiveRecord: 0.1ms)
Started GET "/" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by HomeController#index as HTML
Researcher Load (0.1ms) SELECT "researchers".* FROM "researchers" WHERE "researchers"."id" = ? LIMIT 1 [["id", 2]]
Rendered home/index.html.erb within layouts/application (0.6ms)
Completed 200 OK in 10ms (Views: 8.6ms | ActiveRecord: 0.1ms)
Started GET "/participants/3" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by ParticipantsController#show as HTML
Parameters: {"id"=>"3"}
Participant Load (1.9ms) SELECT "participants".* FROM "participants" WHERE "participants"."id" = ? LIMIT 1 [["id", 3]]
Researcher Load (0.1ms) SELECT "researchers".* FROM "researchers" WHERE "researchers"."id" = ? LIMIT 1 [["id", 2]]
ContactData Load (0.2ms) SELECT "contact_data".* FROM "contact_data" WHERE "contact_data"."participant_id" = ? LIMIT 1 [["participant_id", 3]]
AlcoholService Load (0.1ms) SELECT "alcohol_services".* FROM "alcohol_services" WHERE "alcohol_services"."id" = ? LIMIT 1 [["id", 16]]
Baseline Load (0.3ms) SELECT "baselines".* FROM "baselines" WHERE "baselines"."participant_id" = ? LIMIT 1 [["participant_id", 3]]
Rendered participants/_unlock_contact_data.html.erb (1.1ms)
Rendered participants/_details.html.erb (52.8ms)
Rendered participants/show.html.erb within layouts/application (58.8ms)
Completed 200 OK in 94ms (Views: 69.9ms | ActiveRecord: 3.0ms)
Started POST "/participants/unlock_contact_data?participant_id=3" for 127.0.0.1 at 2017-05-01 18:09:24 +0100
Processing by ParticipantsController#unlock_contact_data as JS
Parameters: {"utf8"=>"✓", "from"=>"show", "password"=>"[FILTERED]", "commit"=>"Submit", "participant_id"=>"3"}
Researcher Load (0.1ms) SELECT "researchers".* FROM "researchers" WHERE "researchers"."id" = ? LIMIT 1 [["id", 2]]
Participant Load (0.0ms) SELECT "participants".* FROM "participants" WHERE "participants"."id" = ? LIMIT 1 [["id", 3]]
ContactData Load (0.1ms) SELECT "contact_data".* FROM "contact_data" WHERE "contact_data"."participant_id" = ? LIMIT 1 [["participant_id", 3]]
AlcoholService Load (0.1ms) SELECT "alcohol_services".* FROM "alcohol_services" WHERE "alcohol_services"."id" = ? LIMIT 1 [["id", 16]]
Baseline Load (0.0ms) SELECT "baselines".* FROM "baselines" WHERE "baselines"."participant_id" = ? LIMIT 1 [["participant_id", 3]]
Rendered participants/_details.html.erb (4.6ms)
Rendered participants/unlock_contact_data.js.erb (6.7ms)
Completed 200 OK in 81ms (Views: 10.8ms | ActiveRecord: 0.3ms)
(0.1ms) rollback transaction
(0.1ms) begin transaction
Is there anything I might be missing?

Every link_to on my rails 4 application is being called twice

I'm experiencing some unusual behaviour with my Rails 4 Application. Every single-time I click on a link_to inside my views, my controllers actions are being called twice. For example:
In my root_url I have this standard call for users_profile:
<%= link_to('User Profile', users_profile_path, :class => "logout-button") %>
When I click this link, my console shows the following output:
Started GET "/users/profile" for 127.0.0.1 at 2013-11-25 20:45:53 -0200
Processing by Users::SessionsController#profile as HTML
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 45 ORDER BY "users"."id" ASC LIMIT 1
InvestorProfile Load (0.5ms) SELECT "investor_profiles".* FROM "investor_profiles" WHERE "investor_profiles"."user_id" = $1 ORDER BY "investor_profiles"."id" ASC LIMIT 1 [["user_id", 45]]
EmployeeProfile Load (0.5ms) SELECT "employee_profiles".* FROM "employee_profiles" WHERE "employee_profiles"."user_id" = $1 ORDER BY "employee_profiles"."id" ASC LIMIT 1 [["user_id", 45]]
Rendered users/sessions/_investor_setup.html.erb (3.9ms)
Rendered users/sessions/profile.html.erb within layouts/application (5.2ms)
Completed 200 OK in 19ms (Views: 11.2ms | ActiveRecord: 2.5ms)
Started GET "/users/profile" for 127.0.0.1 at 2013-11-25 20:45:53 -0200
Processing by Users::SessionsController#profile as HTML
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 45 ORDER BY "users"."id" ASC LIMIT 1
InvestorProfile Load (0.3ms) SELECT "investor_profiles".* FROM "investor_profiles" WHERE "investor_profiles"."user_id" = $1 ORDER BY "investor_profiles"."id" ASC LIMIT 1 [["user_id", 45]]
EmployeeProfile Load (0.2ms) SELECT "employee_profiles".* FROM "employee_profiles" WHERE "employee_profiles"."user_id" = $1 ORDER BY "employee_profiles"."id" ASC LIMIT 1 [["user_id", 45]]
Rendered users/sessions/_investor_setup.html.erb (3.3ms)
Rendered users/sessions/profile.html.erb within layouts/application (4.1ms)
Completed 200 OK in 12ms (Views: 7.5ms | ActiveRecord: 1.2ms)
People often have this behaviour when there's a remote (JS for example) calling the method, but this is not my case. the weirdest part is that, if I put the direct URL to the users_profile_path on my browser. I only get one request on my rails console:
Started GET "/users/profile" for 127.0.0.1 at 2013-11-25 20:48:17 -0200
Processing by Users::SessionsController#profile as HTML
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 45 ORDER BY "users"."id" ASC LIMIT 1
InvestorProfile Load (0.3ms) SELECT "investor_profiles".* FROM "investor_profiles" WHERE "investor_profiles"."user_id" = $1 ORDER BY "investor_profiles"."id" ASC LIMIT 1 [["user_id", 45]]
EmployeeProfile Load (0.2ms) SELECT "employee_profiles".* FROM "employee_profiles" WHERE "employee_profiles"."user_id" = $1 ORDER BY "employee_profiles"."id" ASC LIMIT 1 [["user_id", 45]]
Rendered users/sessions/_investor_setup.html.erb (3.4ms)
Rendered users/sessions/profile.html.erb within layouts/application (4.2ms)
Completed 200 OK in 12ms (Views: 7.7ms | ActiveRecord: 1.1ms)
I'm getting this same result for every link inside my application, not only this one.
Rails 5/6
When I was clicking on a link, the whole controller was being called twice. I tried the accepted answer, but it does not work for me, so I just set turbolinks: false as below:
<%= link_to("Demo", #user, data: { turbolinks: false } ) %>
If you would like your app to still make use of Turbolinks then "Opting out of Turbolinks" on the code that is giving you problems is the way to go; just add data-no-turbolink.
I was having problems with using Bootstrap 3 and adding that fixed it. For example;
<li class="list-group-item" data-no-turbolink>
<%= link_to download_path(item) do %>
<button type="button" class="btn btn-success">Download</button>
<% end %>
</li>
I actually managed to solve this on my own.
There's a default gem that is installed with rails 4.0, it is called Turbolinks*.
For some reason, the javascript used in this gem* was causing the doubled requests on my server. That's why only GET requests were behaving like this, and POST requests were normal.
I still don't fully understand why gem* causes that, but after I removed the following line from my application.js file, the doubled requests stopped.
=// require turbolinks
Another solution is to add data-no-turbolink to the tag.
More info here: http://blog.flightswithfriends.com/post/53943440505/how-to-disable-turbolinks-in-rails-4

Github-like username url

I have this route that let me build custom url for users like
/thisismyname, and it works fine. But when I look at the log there is
something I don't like...
When I hit /gregory this is what is happenning:
1. Going to public_profile#public # Good
2. hitting /assets and trying to find a user with asset token # Not good
I thought my constraints would avoid this but it doesn't seem like it...
class PublicProfileConstraint
def self.matches?(request)
!['assets', 'admin'].include?(request.session[:token])
end
end
get "/:token" => "profiles#public", :as => :public_profile,
:constraints => PublicProfileConstraint
Here is the log:
Started GET "/gregory" for 127.0.0.1 at 2012-03-05 12:44:43 -0800
Processing by ProfilesController#public as HTML
Parameters: {"token"=>"gregory"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE
"users"."id" = ? LIMIT 1 [["id", 1]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE
"users"."token" = 'gregorymarcilhacy' LIMIT 1
Rendered profiles/_modal.haml (0.1ms)
....
Rendered profiles/show.haml within layouts/application (154.7ms)
Completed 200 OK in 431ms (Views: 174.0ms | ActiveRecord: 5.5ms)
... Redering js files ...
# I DONT WANT THIS
Started GET "/assets/" for 127.0.0.1 at 2012-03-05 12:44:45 -0800
Served asset - 404 Not Found (10ms)
Processing by ProfilesController#public as */*
Parameters: {"token"=>"assets"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE
"users"."id" = ? LIMIT 1 [["id", 1]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE
"users"."token" = 'assets' LIMIT 1
Redirected to http://localhost:3000/
Completed 302 Found in 312ms
... Rendering images ...
# AND I DONT WANT THIS
Started GET "/" for 127.0.0.1 at 2012-03-05 12:44:45 -0800
Processing by LandingController#landing as */*
User Load (0.3ms) SELECT "users".* FROM "users" WHERE
"users"."id" = ? LIMIT 1 [["id", 1]]
Rendered landing/landing.haml within layouts/landing (0.8ms)
Completed 200 OK in 288ms (Views: 23.5ms | ActiveRecord: 2.2ms)
You are searching request.session for the token, but this will always fail as that's the session store rather than the request parameters. You probably want the equivalent of params[:token] in the constraint class. The request object documentation indicates that request.path_parameters[:token] might contain the value you are looking for.

Resources