stimulusreflex / stimulus_reflex

Build reactive applications with the Rails tooling you already know and love.

Home Page:https://docs.stimulusreflex.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Stimulus reflex not working making server to stop on ruby on rails

lenku05 opened this issue · comments

i was working on a blog website and i was trying to make my button publish otherwise unpublish but when i ran the server its stops by itself bring me a bunch of lines shown below

    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis/connection/ruby.rb:154:in `connect_addrinfo'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis/connection/ruby.rb:192:in `block in connect'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis/connection/ruby.rb:190:in `each'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis/connection/ruby.rb:190:in `each_with_index'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis/connection/ruby.rb:190:in `connect'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis/connection/ruby.rb:304:in `connect'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis/client.rb:369:in `establish_connection'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis/client.rb:117:in `block in connect'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis/client.rb:328:in `with_reconnect'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis/client.rb:116:in `connect'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis/client.rb:309:in `with_socket_timeout'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis/client.rb:159:in `call_loop'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis/subscribe.rb:44:in `subscription'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis/subscribe.rb:14:in `subscribe'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis.rb:3525:in `_subscription'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis.rb:2334:in `block in subscribe'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis.rb:70:in `block in synchronize'
    from C:/Ruby30-x64/lib/ruby/3.0.0/monitor.rb:202:in `synchronize'
    from C:/Ruby30-x64/lib/ruby/3.0.0/monitor.rb:202:in `mon_synchronize'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis.rb:70:in `synchronize'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis.rb:2333:in `subscribe'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/actioncable-6.1.4/lib/action_cable/subscription_adapter/redis.rb:83:in `block in listen'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis/client.rb:328:in `with_reconnect'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis.rb:76:in `block in with_reconnect'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis.rb:70:in `block in synchronize'
    from C:/Ruby30-x64/lib/ruby/3.0.0/monitor.rb:202:in `synchronize'
    from C:/Ruby30-x64/lib/ruby/3.0.0/monitor.rb:202:in `mon_synchronize'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis.rb:70:in `synchronize'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis.rb:75:in `with_reconnect'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/redis-4.3.1/lib/redis.rb:82:in `without_reconnect'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/actioncable-6.1.4/lib/action_cable/subscription_adapter/redis.rb:80:in `listen'
    from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/actioncable-6.1.4/lib/action_cable/subscription_adapter/redis.rb:154:in `block in ensure_listener_running'

this is my edit.html.erb code

<div class="card-footer">
           <% if @post.published? %>
           <a href="#" class="btn btn-warning col-12"
           data-reflex='click->PublisherReflex#unpublish'
           data-post-id='<%= @post.id %>'>
           Unpublish
         </a>
           <% else %>
           <a href="#" class="btn btn-dark col-12"
           data-reflex='click->PublisherReflex#publish'
           data-post-id='<%= @post.id %>'>
           Publish
         </a>
           <%end%>
         </div>

and this is my publisher_reflex.rb

def publish
     post = Post.find(element.dataset[:post_id])
     post.update(published: true, published_at: Time.now)
   end
   
   def unpublish
     post = Post.find(element.dataset[:post_id])
     post.update(published: false, published_at: nil)
   
   end

Hey there! The stack trace you're seeing suggests that your Rails app is having trouble connecting to your Redis server. What it doesn't tell me is which instance of Redis is failing - but if I had to guess from the reference to SubscriptionAdapter, it's most likely the one defined in config/cable.yml.

Bigger picture, I strongly recommend that you consider doing your development inside of a WSL2 VM so that you can use Ubuntu inside of Windows. This is what I'm doing, and having used Mac, Linux and Windows in the past... Windows with Ubuntu running in WSL2 is the first 10/10 developer experience I've ever had. https://ubuntu.com/wsl

Finally, I recommend that you could re-write your publisher_reflex.rb so that it has a toggle method:

def toggle
  post = Post.find(element.dataset[:post_id])
  post.published? ? post.update(published: false, published_at: nil) : post.update(published: true, published_at: Time.now)
end

Thank you but i have install rails in wsl2 version 18.04 but when i run rails server run the same rails project its telling me
-bash: /mnt/c/Ruby30-x64/bin/rails: ruby: bad interpreter: No such file or directory

Hi again,

It's really hard to help when I don't have access or real visibility to how you have your system set up. It's (very) good that you installed WSL2, but what you're describing is that the Ubuntu VM is attempting to run a Windows version of Ruby from inside Ubuntu.

Your first order of business is to install either rvm https://github.com/rvm/ubuntu_rvm or rbenv https://github.com/rbenv/rbenv so that you can configure whatever version of Ruby that you like - usually either 2.7 or 3.0.

Once you have Ruby set up and running inside of your Ubuntu bash prompt, you should be able to go to the folder for your Rails app (which should be inside of the VM filesystem eg. not mounted with /mnt/c) and run bundle install and then run your Rails app. Hopefully you've already installed Redis and Postgres as well!

Thank you im up and running in wsl2 after bundle install and installing redis

Awesome! Feel free to drop by the StimulusReflex Discord for help.

My publish and unpulish button is still not working im geting an error in the console
stimulus_reflex.js:328 Uncaught TypeError: Cannot read property 'schema' of undefined

@lenku05, this is not a support channel.

Please come ask for help in Discord.