0

My is on Rails 7 with the Devise gem...

I'm implementing a favorite function with a heart svg and the acts_as_votabale gem and that is working as intended when the user is signed in. On my event index page I have:

<h2>Upcoming Events</h2>
   <div class="event-list-wrapper">
       <% @events.upcoming_events.each do |event| %>
            <%= render "event", event: event %>
       <% end %>
   </div> 

In my _event.html.erb I have (notice the user_signed_in Devise helper):

            <% if user_signed_in? %>
                <div class="heart-container">
                    <div id="<%= dom_id= event %>">
                        <%= render "events/favorite-link", event: event %>
                    </div>
                </div>
            <% end %>
            <% if !user_signed_in? %>
                <div class="heart-container">
                   <%= render "events/heart-signup", event: event %>
                </div>
            <% end %>

In the _heart-signup.html.erb I have:

    <svg 
      xmlns="http://www.w3.org/2000/svg" 
      fill="#000000" 
      fill-opacity="0.5" 
      viewBox="0 0 24 24"
      stroke-width="1.2" 
      stroke="#f2f2f2" 
      class="w-6 h-6">
    <path stroke-linecap="round" stroke-linejoin="round" d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12z" />
    </svg>

I was able able to confirm that the <%= render "events/heart-signup", event: event %> does render the image if I enter it directly on the event index page.

In the _favorite-link.html.erb I have:

<%= turbo_frame_tag "#{dom_id(event)}_likes" do %>
  <%= link_to upvote_event_path(event), format: :turbo_stream, method: :patch, remote: true do %>
     <% if current_user.voted_up_on? event %>
            <%= link_to upvote_event_path(event), format: :turbo_stream, method: :patch do %>
               <svg 
                 xmlns="http://www.w3.org/2000/svg" 
                 viewBox="0 0 24 24" 
                 fill="#ff2929" 
                 stroke-width="1.2"
                 stroke="#f2f2f2" 
                 class="w-6 h-6">
               <path d="M11.645 20.91l-.007-.003-.022-.012a15.247 15.247 0 01-.383-.218 25.18 25.18 0 01-4.244-3.17C4.688 15.36 2.25 12.174 2.25 8.25 2.25 5.322 4.714 3 7.688 3A5.5 5.5 0 0112 5.052 5.5 5.5 0 0116.313 3c2.973 0 5.437 2.322 5.437 5.25 0 3.925-2.438 7.111-4.739 9.256a25.175 25.175 0 01-4.244 3.17 15.247 15.247 0 01-.383.219l-.022.012-.007.004-.003.001a.752.752 0 01-.704 0l-.003-.001z" />
               </svg>
            <% end %>
        <% else %>
           <%= link_to upvote_event_path(event), format: :turbo_stream, method: :patch do %>
               <svg 
                 xmlns="http://www.w3.org/2000/svg" 
                 fill="#000000" 
                 fill-opacity="0.5" 
                 viewBox="0 0 24 24"
                 data-user-logged-in="<%= user_signed_in? %>"
                 stroke-width="1.2" 
                 stroke="#f2f2f2" 
                 class="w-6 h-6">
               <path stroke-linecap="round" stroke-linejoin="round" d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12z" />
               </svg>
            <% end %>
      <% end %>
<% end %>
<% end %>

I tried using an else statement first within the same heart-container div before trying to try else with two completely different divs to then now trying two different if statements with user_signed_in? as I listed above, is there something I missing?

As I mentioned above, when I'm logged in, the favorite-link render works just fine, the issue I'm having is when a user is not signed in.

Any insights would be appreciated!

Ogarocious
  • 119
  • 6
  • What about `<% unless user_signed_in? %>`? – Chiperific Nov 28 '22 at 03:04
  • What does `user_signed_in?` return when there is no user signed in? `null` or `false`? – Chiperific Nov 28 '22 at 03:04
  • I tried ```<% unless user_signed_in? %>``` on both lines and I still get the same result of nothing being displayed... Also worth mentioning that I am restarting my server for good measure and I even tried incognito mode as well as a different browser just to make sure if wasn't a cookies issue... How do I test ```user_signed_in?``` it didn't work in the rails console. I know it is a Devise helper. – Ogarocious Nov 28 '22 at 03:36
  • Testing view helpers: Make sure you have the [web-console](https://github.com/rails/web-console) gem installed. Add `<% console %>` in the view below your `if` statements (or just `console` at the end of the the controller action). That will give you a live terminal in your browser where you can test all view helpers. – Chiperific Nov 28 '22 at 03:59
  • And Devise also exposes `current_user`, so check to see what that returns when signed in / not signed in. – Chiperific Nov 28 '22 at 04:01
  • And make sure you're calling `before_action :authenticate_user!` someplace (per [this SO answer](https://stackoverflow.com/questions/6003883/rails-devise-user-signed-in-not-working)) – Chiperific Nov 28 '22 at 04:02

0 Answers0