Am I on the right path building this member invite system?

http://localhost:3000/memorials/mem_DjOYiIPi81Ba3H-john-doe/manage

So a Memorial has_many MemorialInvitation.

To render this page, I go:

# memorial_controller
def show
  @tab = params["tab"]
  if @tab == "manage"
    @memorial_invitation = MemorialInvitation.new
    @pending_memorial_invitations = @memorial.memorial_invitations.pending
    @accepted_memorial_invitations = @memorial.memorial_invitations.accepted
  end
end

# in show.html.erb
<%= render "memorial_invitations/form", memorial: @memorial, memorial_invitation: @memorial_invitation %>

In the memorial_invitations controller, I go:

def create
  @tab = "manage"
  @memorial_invitation = MemorialInvitation.new(
    memorial_invitation_params.merge(
      user_id: current_user.id,
      memorial_id: @memorial.id
    )
  )
  
  respond_to do |format|
    if @memorial_invitation.save
      # Send the invite email to the user.
      token = @memorial_invitation.generate_token_for(:invitation_token)
      UserMailer.with(token: token, memorial: @memorial_invitation.memorial).invite_email.deliver_later

      format.html { redirect_to memorial_tab_path(@memorial, tab: "manage"), notice: "Memorial Invitation was successfully created." }
      format.json { render :show, status: :created, location: @memory }
    else
      @pending_memorial_invitations = @memorial.memorial_invitations.pending
      @accepted_memorial_invitations = @memorial.memorial_invitations.accepted

      format.html { render 'memorials/show', memorial: @memorial, status: :unprocessable_entity }
      format.json { render json: @memory.errors, status: :unprocessable_entity }
    end
  end
end

So notice that I return a render to the memorial_tab_path.

Since I’m using <%= turbo_refreshes_with method: :morph, scroll: :preserve %> in my layout, this entire experience feels nice very SPA. Invitations are added on the fly, errors are shown without reload.

Am I on the right path?

You can probably get more helpful feedback if you’re more specific.

  • For example: “I am getting this error message”
  • Or: “When I do X with Y, I get Z but expected to get Z1…”
  • Also, what is absolutely the smallest piece of code that you can create that is required to re-create your issue? (MRE)

Not my favorite place to ask questions, but the SO post on how to ask a good question is relevant here: How do I ask a good question? - Help Center - Stack Overflow

2 Likes

The feature itself is working fine. I was just asking if this is the best way to approach this with hotwire Rails 8 morph features.

Yes, you are on the right path.