Rails Model not saving to database

I think I have the right understanding of what's NOT happening. Essentially is happening is I've created a model that would create an "Event" underneath a "Meet". Anytime I attempt to create a new Event, all the inputs are set to 'nil'.
The database seems to be working. I created users using Devise and I'm able to login and see the users in Rails Console.

**event.rb**

class Event < ApplicationRecord has_many :meets has_many :users, through: :meets enum style: { "Choose Style" => 0, "Backstroke" => 1, "Freestyle" => 2, "Butterfly" => 3, "Breaststroke" => 4 }
enum distance: { "Choose Distance" => 0, "25 Yards" => 1, "50 Yards" => 2, "100 Yards" => 3, "200 Yards" => 4 }
end

**event_controller.rb**

class EventsController < ApplicationController before_action :find_event, only: [:edit, :update, :show, :delete] def index @events = Event.all end def new @event = Event.new end def create @event = Event.new if @event.save(event_params) flash[:notice] = "Successfully creaeted event" redirect_to event_path(@event) else flash[:alert] = "Error creating new event" render :new end end def edit end def update if @event.update_attributes(event.params) flash[:notice] = "Successfully updated event" redirect_to event_path(@event) else flash[:alert] = "Error updating event... Try again" render :edit end end def show end def destroy if @event.destroy flash[:notice] = "Successfully deleted event." redirect_to events_path else flash[:alert] = "Error Deleting event!" end end private def event_params params.require(:event).permit(:style, :distance, :seed_time, :overall_time, :heat_place, :overall_time, :team, :points, :swim_style_type, :distance_type) end def find_event @event = Event.find(params[:id]) end **_form.html.erb** <%= simple_form_for @event do |f| %>
<% if @event.errors.any? %>

<%= "#{pluralize(@event.errors.count, "error")} prohibited this post from being saved:" %>

    <% @event.errors.full_message.each do |msg| %>

  • <%= msg %>
  • <% end %>

<% end %>

<%= f.select :style, Event.styles.keys %>
<%= f.select :distance, Event.distances.keys %>
<%= f.input :seed_time, class: "form-control" %>
<%= f.input :overall_time, class: "form-control" %>
<%= f.input :heat_place, class: "form-control" %>
<%= f.input :overall_place, class: "form-control" %>
<%= f.input :team, class: "form-control" %>
<%= f.input :points, class: "form-control" %>
<%= f.button :submit, class: "btn btn-primary" %>

<% end %>

**Rails Console Output**

Any help would be appreciated. Thank you!

CD

1 thought on “Rails Model not saving to database”

  1. I think it’s your `create` action in **event_controller.rb**
    You are passing the `event_params` to `#save` but they should be passed to `#new`.

    Try this:

    def create
    @event = Event.new(event_params)
    if @event.save
    flash[:notice] = “Successfully creaeted event”
    redirect_to event_path(@event)
    else
    flash[:alert] = “Error creating new event”
    render :new
    end
    end

    Reply

Leave a Comment