Stuck with hotwire turbo stream in Rails 7

64 Views Asked by At

I'm using Turbo Stream in Rails 7 to display the content of my "new" form within a modal on the index page. Initially, it works perfectly - clicking the "add" button on the index page opens the modal, and I can successfully submit the form. The modal closes, the new item is displayed on the index page and I remain on the index page.

However, upon attempting the same process again, clicking the "add" button redirects me to the "new" page with the modal, rather than staying on the index page as desired.

To resolve this, I need to reload the index page before clicking the "add" button again.

Do you know what i did wrong ?

This is my code : index.html.erb :

\<%= link_to new_year_myfifteen_path(@year), remote: true, data: { turbo_frame: "modal" } do %\>
\<%= image_tag "add_miss.svg" %\>
\<% end %\>

new.html.erb

\<%= turbo_frame_tag "modal" do %\>

\<%= render "myfifteens/form", myfifteen: @myfifteen %\>

\<% end %\>

_form.html.erb

<h1>Choisir une miss</h1>
<% if myfifteen.errors.any? %>
  <div class="alert alert-danger">
    <%= myfifteen.errors.full_messages.to_sentence %>
  </div>
<% end %>

<%= form_tag new_year_myfifteen_path(@year), method: :get do %>
  <%= select_tag 'category', options_for_select(@categories, @selected_category), include_blank: 'Select category' %>
  <%= submit_tag 'Filter' %>
<% end %>

  <button class="carousel-control-prev" type="button" data-bs-target="#carouselExample" data-bs-slide="prev">
  <span class="carousel-control-prev-icon" aria-hidden="true"></span>
  <span class="visually-hidden">Previous</span>
  </button>
  <button class="carousel-control-next" type="button" data-bs-target="#carouselExample" data-bs-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="visually-hidden">Next</span>
  </button>

<div id="carouselExample" class="carousel slide">
  <div class="carousel-inner">
    <% @misses.each do |miss| %>
      <div class="carousel-item <%= 'active' if miss == @misses.first %>">
        <div class="avatar-item">
          <%= miss.region %>
          <%= image_tag "#{miss.photo}.jpg"%>
          <%= simple_form_for([@year, @myfifteen]) do |f| %>
           <%= f.input :my_miss_france_guess, as: :boolean, label: "C'est elle la Miss France!" %>
           <%= f.hidden_field :miss_id, value: miss.id %>
           <%= f.button :submit, "Valider" %>
          <% end %>
        </div>
      </div>
    <% end %>
  </div>



</div>

create.turbo_stream.erb

\<%= turbo_stream.prepend "myfifteens", @myfifteen %\>

\<%= turbo_stream.replace "modal", "" %\>

myfifteen controller :

class MyfifteensController \< ApplicationController

    def index
      @year = Year.find(params[:year_id])
      @myfifteens = Myfifteen.where(user_id: current_user, year_id: @year.id).to_a
      all_my_groups = current_user.groups.to_a
      @this_year_groups = all_my_groups.select { |group| group.year_id == @year.id }
      @my_top_15 = Myfifteen.where(user_id: current_user.id).pluck(:miss_id).uniq
    end
    
    def new
      @myfifteen = Myfifteen.new
      @year = Year.find(params[:year_id])
      @misses = Miss.where(year_id: @year.id)
                    .joins("LEFT JOIN myfifteens ON misses.id = myfifteens.miss_id AND myfifteens.user_id = #{current_user.id}")
                    .where("myfifteens.id IS NULL")
      @categories = Category::CATEGORIES
      @selected_category = params[:category]
    
      if @selected_category.present?
        @misses = @misses.joins(:categories).where(categories: { critere: @selected_category })
      end
    
    end
    
    
    def create
      @year = Year.find(params[:year_id])
      @myfifteen = Myfifteen.new(myfifteen_params.merge(user_id: current_user.id, year_id: @year.id))
    
    
      @myfifteen.miss = Miss.find(params[:myfifteen][:miss_id])
      @myfifteen.user_id = current_user.id
      @myfifteen.year_id = @year.id
      existing_guess = Myfifteen.find_by(user_id: current_user.id, my_miss_france_guess: "1")
      existing_guess.update(my_miss_france_guess: "0") if existing_guess.present?
    
      if  @myfifteen.save!
        # Reassign @all_my_fifteens after creating a new Myfifteen record
        @myfifteens = Myfifteen.where(user_id: current_user, year_id: @year.id).to_a
        # Add debug output
        puts "@all_my_fifteens after reassignment: #{@myfifteens.inspect}"
    
        respond_to do |format|
          format.html { redirect_to year_myfifteens_path(@year) }
          format.turbo_stream
        end
    
    
      else
        render :new, status: :unprocessable_entity
      end
    end
    
    def show
      @myfifteen = Myfifteen.find(params[:id])
    end
    
    
    private
    
    def myfifteen_params
      params.require(:myfifteen).permit(:year_id, :user_id, :miss_id, :my_miss_france_guess)
    end

end
0

There are 0 best solutions below