Rails: Turbo Stream Broadcast not updating view - ruby-on-rails

I have a Turbo Stream which is not updating the view and I'm not sure why. I have two other broadcasts setup in a similar configuration which are working fine.
Everything looks like it should be working from what I can see, I'm just not getting the update on the front end. Am I missing something obvious?
# partial
<%= turbo_stream_from 'team_players' %>
<div id="players_<%= dom_id(team)%>"
<% team.players.each do |player| %>
<%= render player %>
<% end %>
</div>
# player model
class Player < ApplicationRecord
belongs_to :team
after_create_commit do
broadcast_prepend_to(
'team_players',
target: "players_team_#{team.id}",
locals: { player: self }
)
end
end
# server log
Started GET "/cable" for 127.0.0.1 at 2022-02-16 11:45:13 +0000
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2022-02-16 11:45:13 +0000
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-alive, Upgrade, HTTP_UPGRADE: websocket)
Turbo::StreamsChannel is transmitting the subscription confirmation
Turbo::StreamsChannel is streaming from team_players
# console log after manually creating a player for team 9 (exert)
...
Rendered players/_player.html.erb (Duration: 0.3ms | Allocations: 99)
[ActionCable] Broadcasting to team_players: "<turbo-stream action=\"prepend\" target=\"players_team_9\"><template><div id=\"player_1885\">\n <p>\n <strong>ID:</strong>\n 1885\n </p>\n\n <p>\n <strong>User:</strong>\n 3820748f-d9d3-400e-ac90-6149800a0e68\n </p>\n\n <p>\n <strong>Team:</strong>\n 9\n </p>\n\n <p>\n ...
=>
#<Player:0x00007fba5fa12568
...
Any help or pointers are appreciated!

It looks as though I had missed Step 4 in the turbo-rails installation process: https://github.com/hotwired/turbo-rails#installation
After running: ./bin/rails turbo:install:redis everything worked from the command line.
Without that step, things still worked from a separate browser session.
Thanks to David Colby on Twitter for finding the fix.

Related

How to create a websocket with a socket.io client and a rails server?

I am trying to create an app with ruby on rails. I use vue-js as a client and rails as a Server (they are seperate) and I would like to create a websocket but I am struggling really hard... I use socket-io to do that, but I am completely lost to the point of creating a subscription to a channel. Do you have any way to use a rails server (cross origins) with vue-js ?
client-side :
this.ws = new io("ws://localhost:3000",
{
path: "/cable",
transports: ["websocket"],
upgrade: true
});
this.ws.on("error", (error) => {
console.log(error)
});
this.ws.on("connect", () => {
this.connected()
});
this.ws.on("disconnect", () => {
this.disconnected()
});
server-side:
#config/environments/development.rb
config.action_cable.allowed_request_origins = ['/(\.dev$)|^localhost$/']
config.action_cable.url = "ws://localhost:3000/cable"
#routes.rb
Rails.application.routes.draw do
mount ActionCable.server => '/cable'
end
My channel :
class TheChannel < ApplicationCable::Channel
def subscribed
stream_from "Thechannel"
puts "A user is subscribed "
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
what I get on my server's console:
Started GET "/cable/?EIO=4&transport=websocket" for ::1 at 2022-
10-15 00:07:28 +0200
Started GET "/cable?EIO=4&transport=websocket" [WebSocket] for
::1 at 2022-10-15 00:07:28 +0200
Successfully upgraded to WebSocket (REQUEST_METHOD: GET,
HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Finished "/cable?EIO=4&transport=websocket" [WebSocket] for ::1
at 2022-10-15 00:07:28 +0200
The connection is finished right after it created

Impossible to delete a flash

I made a mistake before migrating a plugin, and have written
flash[:notice] = :label_presta_added
instead of
flash[:notice] = l(:label_presta_added)
I corrected my mistake but it seems that my Redmine Plugin has trashed my Redmine. Even though I delete my plugin a migrate once again, I still get this error:
Started GET "/" for 127.0.0.1 at 2016-06-01 22:21:37 +0200
Processing by WelcomeController#index as HTML
Current user: admin (id=1)
Rendered welcome/index.html.erb within layouts/base (28.1ms)
Completed 500 Internal Server Error in 366ms (ActiveRecord: 116.0ms)
ActionView::Template::Error (undefined method `html_safe' for :label_presta_added:Symbol
Did you mean? html_safe?):
97: <div id="sidebar">
98: <%= yield :sidebar %>
99: <%= view_layouts_base_sidebar_hook_response %>
100: </div>
101:
102: <div id="content">
103: <%= render_flash_messages %>
app/helpers/application_helper.rb:312:in `block in render_flash_messages'
app/helpers/application_helper.rb:311:in `render_flash_messages'
app/views/layouts/base.html.erb:100:in `_app_views_layouts_base_html_erb__4104276684161420982_39604440'
lib/redmine/sudo_mode.rb:63:in `sudo_mode'
Can somebody give me a hand here?
Thanks in advance!
This is stored in your session, so usually changing the session secret key will invalidate all sessions and discard any old session data.
You can also try and rescue to clear it out as a one-time deal.
Have you restarted the server? Or you can use flash[:notice] = nil to remove it.
It looks like it throws a html_safe error. Can you see if the method which is rendering the flash is using html_safe? It looks like its coming from there.
Not sure exactly, may be shooting in the dark.
But read these and try may be:
actionview::template::error(undefined method 'html_safe' for nil:NilClass)
http://www.redmine.org/issues/8477

Error with JS.ERB file

I'm new into Rails world, and I'm trying to learn.
I'm following these tutorial: http://www.noupe.com/development/create-a-simple-twitter-app.html and trying to create an simple twitter application.
All code can be found in https://github.com/ricardovsilva/twitter-rubyOnRails .
I'm trying to add some ajax and javascript functionallity to my app and I'm having a lot of issues (and some of they I'm able to deal with google searchs) because tutorial is in rails 2 and I'm using rails 4.
The line of tutorial I'm stucked is:
Finally, we need to create the rjs template. To do this, create a file called “create.js.rjs” in the views/posts folder and add the following code.
page.insert_html :top, :posts, :partial => #post
page[#post].visual_effect :highlight
The problem is, when my browser try to render the page that uses this JS, I get the following error : "Uncaught SyntaxError: Unexpected token"
By the way, I'm using google chrome. To simulate the error just clone the repo, access this and run rails server. Open browser and go to http://localhost:3000/posts . Page will render, but with one error (posts will not be loaded, only one textbox and one button).
I really don't know what to do to make the browser understand these rails commands.
Any help will be appreciated.
EDIT: This is all that appears on my terminal
=> Booting WEBrick
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run rails server -h for more startup options
=> Ctrl-C to shutdown server
[2015-02-28 15:01:33] INFO WEBrick 1.3.1
[2015-02-28 15:01:33] INFO ruby 2.1.5 (2014-11-13) [x64-mingw32]
[2015-02-28 15:01:33] INFO WEBrick::HTTPServer#start: pid=5872 port=3000
Started GET "/posts" for ::1 at 2015-02-28 15:01:39 -0300
ActiveRecord::SchemaMigration Load (1.0ms) SELECT schema_migrations.* FROM
schema_migrations
Processing by PostsController#index as HTML
Rendered posts/_message_form.html.erb (2.0ms)
Post Load (1.0ms) SELECT posts.* FROM posts ORDER BY posts.created_at
ASC
Rendered posts/_post.html.erb (13.0ms)
Rendered posts/index.html.erb within layouts/posts (52.0ms)
Completed 200 OK in 120ms (Views: 78.0ms | ActiveRecord: 16.0ms)
Started GET "/assets/create-3acb657a1c6a2ce4ccc5172e5bd59c6e.js?body=1" for ::1
at 2015-02-28 15:01:41 -0300
The file where error happens is the create.js.erb, that is saved into folder app/assets/javascript.
This is the content of file:
page.insert_html :top, :posts, :partial => #posts
page[#post].visual_effect : highlight
[Edit 2 - After #rubykid suggestion]
I'm calling these javascript (or trying) from the posts.html.erb, but now I'm having error on console.
<!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<%=javascript_include_tag :all%>
</head>
<body>
<div id="content">
<%= yield %>
</div>
</body>
</html>
It's appear like he's trying to get the "all.js" file, and he will not, because it's don't exists. (on terminal I get this line Started GET "/javascripts/all.js" for ::1 at 2015-02-28 15:44:04 -0300)
What is the correct syntax in ruby 4?

ember/rails app loading not loading properly (takes too long) in development

i have a new ember-cli app with rails backend. it is taking about 30+ seconds to load in development... which is obviously way too long.
i am using the ember-cli-rails gem https://github.com/rwz/ember-cli-rails
i'm using the rails server (i've tried webrick and thin). i'm not using ember server, not running them separately.
i am not using the rails asset pipeline. i have deleted it.
here is my rails config/routes
Rails.application.routes.draw do
resources :rewrites, contraints: { format: :json }
resources :users, contraints: { format: :json }
resources :movies, contraints: { format: :json }
root 'application#index'
namespace :api do
get :csrf, to: 'csrf#index'
end
get "/*path" => "application#index", contraints: { format: :html }
here is my application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Better Films</title>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
here is my application/index.html.erb, frontend is the name of my ember app
<%= include_ember_script_tags :frontend %>
<%= include_ember_stylesheet_tags :frontend %>
when i go to localhost, my rails server log shows this
Rendered application/index.html.erb within layouts/application (173.6ms)
Completed 200 OK in 203ms (Views: 202.6ms | ActiveRecord: 0.0ms)
Started GET "/assets/frontend/frontend.css?body=1" for 127.0.0.1 at 2015-01-15 23:13:28 -0600
Started GET "/assets/frontend/vendor.css?body=1" for 127.0.0.1 at 2015-01-15 23:13:39 -0600
Started GET "/assets/frontend/frontend.js?body=1" for 127.0.0.1 at 2015-01-15 23:13:49 -0600
Started GET "/assets/frontend/vendor.js?body=1" for 127.0.0.1 at 2015-01-15 23:14:00 -0600
web inspector network tab shows
I'm not sure if this is related, but I had the same issue when I set the ember app's path in config/initializers/ember.rb to Rails.root.join('frontend').to_s. When I removed the path option, everything worked fine.

My route returns a blank view (no html when I do view source)

I am brand new to Ruby on Rails and I have been trying to get a simple default route set up and working. When I try to run my application I get a blank result (if I do a view source, there is nothing there).
Here are the relevant files (not sure if I am missing something that would be useful).
app/config/routes.rb
Blog::Application.routes.draw do
root :to => "home#index"
end
app/views/home/index.html.erb
<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
end
end
app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Benji</title>
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
When I try to run my application, I go to http://localhost:3000 and nothing shows up. If I do a view source, it is empty.
If do rake routes this is the result:
JESSE-GAVINs-MacBook-Pro-17:benji jesse$ rake routes
(in /Users/jesse/Dev/benji)
root /(.:format) {:action=>"index", :controller=>"home"}
JESSE-GAVINs-MacBook-Pro-17:benji jesse$
In my development.log file I see this:
Started GET "/" for 127.0.0.1 at Tue Sep 07 10:44:10 -0500 2010
Processing by HomeController#index as HTML
Rendered home/index.html.erb within layouts/application (2.8ms)
Completed 200 OK in 15ms (Views: 14.7ms | ActiveRecord: 0.0ms)
What could be the issue? How do I go about solving this?
Solved (sort of...)
It works just fine when I started the rails server on a different port.
It was my understanding that I didn't need to restart the server in order for it to detect changes in the code.
Sorry about the waste of time, I just didn't know which tree I needed to bark up. I learned some things along the way.
As you're playing with Routes, you absolutely need to restart the server. I don't know if this is part of the problem, but most (all?) things under config/ need the server to be restarted to take effect, particularly routes and environment

Resources