CodeWalrus
CodeWalrus

Reputation: 6068

When I submit content in Rails 4, an alert incorrectly says there are two errors?

When submitting a form (title and content that should pass), I get the following:

enter image description here

Next, in events.rb, I commented out the validation requirements:

class Event < ActiveRecord::Base
    has_many :comments, dependent: :destroy
    #validates :title, presence: true,
    #                length: { minimum: 5 }
end

After that, the form would let me submit an event post, but everything was blank, as shown:

enter image description here

As a note, my submissions were working until around the time I submitted Devise, and made a few other changes with adding a user model. Please take a look and let me know what I can do about this.

Upvotes: 0

Views: 53

Answers (3)

Kirti Thorat
Kirti Thorat

Reputation: 53028

Looking at your EventsController in git repo, I see that in your create action you are creating the event instance as follows:

    @event = Event.new(params[:event].permit(:title, :text))

Change that to

    @event = Event.new(event_params)

Or

    @event = Event.new(params.require(:event).permit(:title, :text))

you can check it by turning on the validations, It should work now.

Upvotes: 0

OneChillDude
OneChillDude

Reputation: 8006

In your events_controller.rb, you need to permit the desired user-editable parameters, and create your event with that.

Event.create!(event_params)

def event_params
  params.require(:event).permit(:title, :description)
end

Upvotes: 1

sonnyhe2002
sonnyhe2002

Reputation: 2121

If you don't want it to be blank, then you will still need

validates :title, presence: true

Upvotes: 0

Related Questions