stimulusreflex / stimulus_reflex

Build reactive applications with the Rails tooling you already know and love.
https://docs.stimulusreflex.com
MIT License
2.28k stars 172 forks source link

Current Object in reflex #619

Closed omarluq closed 1 year ago

omarluq commented 1 year ago

Im working with the current object, in my code I have

user_controller.rb

class UserController  < ApplicationController

  def new
    return if @stimulus_reflex
    Current.user = User.new
    @user =  Current.user
  end
end

new.html.erb

<%= form_with model: @user, data: { serialize_form: true } do |f| %>
  <%= f.input_field :name, data: { reflex: "input->User#on_change" } %>
<% end %>

user_reflex.rb

class UserReflex < ApplicationReflex

  def on_change
    @user = Current.user
  end
end

Current.user is returning nil in the reflex ? I'm not sure if this is an expected behavior or if I'm missing something the docs doesn't go deep into the Current object. any help would be appreciated

on a separate note this gem is AWESOME!!

leastbad commented 1 year ago

Howdy, Omar! Are you on Discord? This is an excellent Discord question. 😉

Give this a shot: https://docs.stimulusreflex.com/rtfm/patterns#the-current-pattern

If you have any problems whatsoever, just jump on the server and there's folks who will help you 24/7.