0

I have a turns model that i want to create based off a card id and a user id, the user id is passed correctly, but the card id is nil.

This is the card turbo frame

  <%= turbo_frame_tag "play_card_frame" do %>
    <%= render "turns/form", turn: @turn, card_id: card.id %>
  <% end %>

It renders the following form

<%= simple_form_for @turn,
                    html: {
                      class: "turn form",
                      data: { turbo: false },
                    } do |f| %>  
  <% if @turn.errors.any? %>
    <div class="error-message">
      <%= @turn.errors.full_messages.to_sentence.capitalize %>
    </div>
  <% end %>

    <%= f.hidden_field :games_user_id %>
    <%= f.hidden_field :card_id, value: params[:card_id] %>

    <%= f.submit "Play", class: "btn btn--secondary" %>

<% end %>

this is the controller for the Turn model

  def new
    @turn = Turn.new(dealts_params)
  end

  def create
      @turn = Turn.new(turns_params)
      @turn.card_id = params[:turn][:card_id]

      if @turn.save
        respond_to do |format|
          format.html { redirect_to game_path(current_user.game_id), notice: "You played a card." }
          format.turbo_stream do
            render turbo_stream: turbo_stream.append(
              "play_card_frame",
              partial: "form",
              locals: { game: @turn }
            )
          end
        end
      else
        render :new
      end

  end


  private

  def turns_params
    params.require(:turn).permit(:games_user_id, :card_id)
  end

and this is the form error i get

Card can't be blank and card must exist

0 Answers0