MLZ
MLZ

Reputation: 433

Rails form params not being picked up

I'm doing a small project to get familiar with Rails. I have the following code:

Model event.rb

class Event < ApplicationRecord
  validates :title, presence: true
  validates :date, presence: true
  has_and_belongs_to_many :users
end

View app/views/events/new.html.erb

<h1> Create event </h1>
<p> Create a new event here: </p>
<%= form_for @event do |e| %>
<%= e.label :title %>
<%= e.text_field :title %> <br />
<%= e.label :date %>
<%= e.text_field :date %> <br />
<%= e.submit  %>
<% end %>

Controller app/controllers/events_controller.rb

def create
    @event = Event.new(event_params)
    if @event.save
      render html: "Event saved!"
    else
      render html: "Didn't work :("
    end
  end

 private
  def event_params
    params.permit(:title, :date)
 end

Routes

  get "events"      => "events#index"
  get "events/new"  => "events#new"
  post "events"     => "events#create"

However each time I submit, the params is coming up empty: Empty params

I didn't use resource :events and instead doing it manually to practice. Why isn't params being picked up?

Upvotes: 4

Views: 60

Answers (1)

Matias Carpintini
Matias Carpintini

Reputation: 137

Try this:

params.require(:event).permit(:title, :date)

Upvotes: 3

Related Questions