ViewComponent / view_component

A framework for building reusable, testable & encapsulated view components in Ruby on Rails.
https://viewcomponent.org
MIT License
3.28k stars 429 forks source link

Stimulus Reflex doesn't render ViewComponent with params #432

Closed alliedarmour closed 4 years ago

alliedarmour commented 4 years ago

Steps to reproduce

In my reflex I want to re-render a view component with the morph directive, like so:

 def order_success(user_id)
    morph "#weekly-meal-list, ApplicationController.render(MealComponent.new(user_id: user_id.to_i))
  end

Then my view component is initialized with the user record like this:

  def initialize(user_id:)
    @user = User.find(user_id)
  end

I tried to pass the user instance directly aswell, but still same issue. Now, in my view (cause of wicked pdf) I have to render the component with the render_component directive instead of just render.

<tbody id="weekly-meal-list">
      <%= render_component(MealComponent.new(user_id: user.id)) %>
</tbody>

Now, when I want to re-render just this table with the reflex above, I get the following error:

StimulusReflex::Channel Failed to invoke Order#order_success! http://localhost:3000/users/1/order_cards/new '#<MealComponent:0x000055fe47a95e98 @user=#<User id: 1, name: "Admin User", email: "example@braunfels.org", password_digest: "$2a$10$sx3fPbyQoPfIP4j/iIiGeOYbNw2laFIE.iVwzCxP7ts...", twoFactorEnabled: "enabled", canDoGuestOrder: true, commentedToday: true, hasCommentedThisWeek: true, otp_secret_key: "ZZOJYZGT6ENI2HADWXPELKVAGREEW2VS", created_at: "2020-07-18 11:36:03", updated_at: "2020-07-24 17:18:03", reset_digest: nil, reset_sent_at: nil, workplace_id: 2, has_ldap_connection: nil, salt: "zhxBMJfAmPVW4MiXvCu3", remember_me_token: "T1fgN4zhqdW17juCH6K4", remember_me_token_expires_at: "2020-08-06 07:51:43", user_accountable_type: "ExternalClinicAccount", user_accountable_id: 1>>' is not an ActiveModel-compatible object. It must implement :to_partial_path. /home/mkrs/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionview-6.0.3.2/lib/action_view/renderer/partial_renderer.rb:493:inpartial_path'`

But as I checked inside the initialize method of the component, it IS implementing :to_partial_path:

(byebug) user.respond_to?(:to_partial_path)
true

Expected behavior

Just the view component should get rendered again, with the new updated list.

Actual behavior

The view component doesn't get rendered, instead I get the error.

System configuration

Rails version: 6.0.3.2

Ruby version: 2.6.5

Gem version: 2.18.0

joelhawksley commented 4 years ago

@alliedarmour thank you for taking the time to file this issue. Would you be willing to write a PR with a failing test for this problem?

scottbarrow commented 4 years ago

@alliedarmour did you figure this out, having a similar issue.

andrewmcodes commented 4 years ago

I believe view_component_reflexwas written to address this.

alliedarmour commented 4 years ago

@scottbarrow sorry for the late response, yeah I figured out what caused the problem. I mentioned it in the Stimulus Reflex repo over here.

@andrewmcodes This looks awesome, I will give it a try!

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.