Search code examples
ruby-on-railsform-forbelongs-to

Create Button in rails app isn't working. Why won't it save to the db?


So this may be a duplicate but I am having a lot of issues fixing it. I am new to rails so it could easily be something minor. I am making a workout log and am able to successfully save new data in the console but then when I go to the server it doesn't work.

User Model:

class User < ApplicationRecord
  has_secure_password
  has_many :workouts, dependent: :destroy

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i.freeze
  validates :name, :email, presence: true
  validates :email, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
end

Workout Model:

class Workout < ApplicationRecord
  validates :date, presence: true

  belongs_to :user
  has_many :exercises, dependent: :destroy
  accepts_nested_attributes_for :exercises, allow_destroy: true, reject_if: proc{ |attr| attr['name'].blank?}
end

Workout Controller (some methods removed):

class WorkoutsController < ApplicationController
  before_action :require_login

  def new
    @workout = current_user.workouts.build(date: Date.today)
  end

  def create
    @workout = current_user.workouts.build(workout_params)
    if @workout.save
      redirect_to workouts_path, notice: 'Workout was successfully created.'
    else
      @errors = @workout.errors.full_messages
      redirect_to new_workout_path, notice: @errors
    end
  end

  private

  def workout_params
    params.fetch(:workout, {}).permit(:date, exercises_attributes: %i[_destroy id name reps weight weight_unit hours minutes seconds])
  end
end

_form (removed exercises nested form):

<div class="container-fluid">
  <%= form_with(model: @workout, local: true, authenticity_token: true) do |form| %>
    <%= hidden_field_tag :authenticity_token, form_authenticity_token -%>
    <% if @workout.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(@workout.errors.count, "error") %> prohibited this workout from being saved:</h2>

        <ul>
          <% @workout.errors.full_messages.each do |message| %>
            <li><%= message %></li>
          <% end %>
        </ul>
      </div>
    <% end %>

    <div class="container-fluid align-items-center">
      <form>
        <div class="container-fluid">
          <%= form_for @workout do |f| %>
            <div class="form-group">
              <h2><%= f.label :date %></h2>
              <div style="font-size: 18pt"><%= f.date_field :date %></div>
            </div>
          <% end %>
        </div>
        <div class="actions" >
          <%= link_to "Create Workout", workouts_path, :method => :post %>
          <%= form.submit %>
        </div>
      </form>
    </div>
  <% end %>
</div>

Please let me know if you see anything off or have any suggestions on what to try. I did notice that I can save the workout if it is all done in the new method but not by clicking the button. I have two submit buttons because I was testing different things on each.

Thanks in advance and let me know if you want me to post more info.


Solution

  • You have manually added <form>...</form> tags to your html, here:

    <form>
      <div class="container-fluid">
        <%= form_for @workout do |f| %>
          <div class="form-group">
            <h2><%= f.label :date %></h2>
            <div style="font-size: 18pt"><%= f.date_field :date %></div>
          </div>
        <% end %>
      </div>
      <div class="actions" >
        <%= link_to "Create Workout", workouts_path, :method => :post %>
        <%= form.submit %>
      </div>
    </form>
    

    Don't do that. form_for creates your form tags for you.