0

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.

CodeWalrus
  • 5,628
  • 1
  • 16
  • 18

3 Answers3

1

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
OneChillDude
  • 7,856
  • 10
  • 40
  • 79
  • Hi Brian, thanks for the help. I tried this but it did not work. I made the assumption to put `@event = Event.create!(event_params)`, but the outcome was same as before. I also tried w/o the `@event` and got a red-screen error saying the same thing about 2 errors. I had "event params" in the private area of the controller from before, so did not make changes to that. Anything else I might be missing? – CodeWalrus Feb 16 '14 at 04:03
0

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

validates :title, presence: true
sonnyhe2002
  • 2,091
  • 3
  • 19
  • 31
  • Hi, I tried this but it still gives me the title error (ie it says there is no title.) Any other possibilities I may have missed? – CodeWalrus Feb 16 '14 at 04:09
0

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.

Kirti Thorat
  • 52,578
  • 9
  • 101
  • 108
  • Hi Kirti, I tried this but it did not work. Thanks for checking out my Git repo. Unfortunately I got the exact same errors as before, trying both of your suggestions. Any other possibilities you might know of? – CodeWalrus Feb 16 '14 at 04:06
  • Do you mean that you are inputting a title with length more than 5 i.e., a perfectly valid title which should pass the validations then submitting the form and you get the same error saying "Title can't be blank"? – Kirti Thorat Feb 16 '14 at 04:18
  • Yes, I can put a whole book and it keeps saying I am doing the wrong thing. It says 2 errors, cannot be blank and 'is less than 5 characters.' I think it has something to do with my inclusion of the gem 'protected-attributes.' [This link](https://teamtreehouse.com/forum/build-a-simple-ruby-on-rails-application-generating-the-user-model-problems-every-time-i-enter-a-email-and-password-it-says-its-blank) seems to address what I mean, I'm trying to figure it out right now. – CodeWalrus Feb 16 '14 at 04:49
  • I do think it is the attr_accessible, but I get `attr_accessible is extracted out of Rails into a gem. Please use new recommended protection model for params(strong_parameters) or add protected_attributes to yourGemfile to use old one. (RuntimeError)`. I will try to fix that without using the gem. Do you know how I might do that easily, given the provided info? – CodeWalrus Feb 16 '14 at 04:54
  • Attr_accessible was removed from Rails4 due to the introduction of strong parameters. I am not sure why you are using that gem because I don't think it's required if you are using Rails4. You can refer to this question for details : (http://stackoverflow.com/questions/17371334/how-is-attr-accessible-used-in-rails-4) – Kirti Thorat Feb 16 '14 at 05:20
  • @CodeWalrus is your problem resolved? Or still there? – Kirti Thorat Feb 16 '14 at 13:33
  • Hi Kirti, I'll update my original post with replies from now on, since S.O is saying that the comments are getting a bit long. For now, I excluded the line `attr_accessible :email, :password, [...]`. Now it isn't broken, but might be unprotected from mischevious users. I don't have a user controller yet, so can't do certain parameter changes (as in your link). I installed the gem because Rails said to, if I wanted to use attr accesible. A tutorial I'm doing wants attr, but the tut uses Rails 3.2, so I'm trying to adapt it. – CodeWalrus Feb 16 '14 at 20:26
  • If you want to use `attr_accessible` as required by the tutorial, then you can create a new gemset(using rvm) for your application by installing Rails 3.2 gem in it. – Kirti Thorat Feb 17 '14 at 18:06