views:

153

answers:

1

So, I'd like for a user to see an error message if he submits a comment and the :name is blank (typical error message, don't need help with that). However, I'd then like to allow the user to skip that validation once he's been notified that "we like all comments to have a name." So, he submits the comment once, sees the notification, then can submit the form again unchanged if he really doesn't want to add a name, and the validates_presences_of :name is skipped.

But, I'm not sure how to go about doing this. I thought about checking to see where the request is coming from, but after a create, errors are handed off to the "new" action, which is the same as actual "new" comments.

I then thought about checking to see if flash[errors] were present, but that won't work because there are other validations a comment has to pass.

Finally, I thought about trying a

validates_presences_of :name, :unless => :notified

but wasn't sure how to define notified. I honestly hate asking such an open ended question, but wasn't sure where to get started. So, is there a way to just check a certain validation once?

Edit: here's my controller

@comments = params[:comments].values.collect{ |comment| current_user.comments.create(comment) }.reject { |p| p.errors.empty? }

and my view:

<% @comments.each_with_index do |comment, index| %>
   <% fields_for "comments[#{index}]", comment do |f| %>
      <%= render :partial => 'form', :locals => { :f => f } %>
   <% end %>
<% end %>

(the stuff in the 'form' partial is just the basic f.label, f.text_area stuff)

I'm thinking the problem is really in my view, since it handles an array of comments. Should the notified hidden field be defined in the 'form' partial, or in this "new" view? My thinking is that each iteration of the partial doesn't need a notified hidden_field, but only the overall form itself. But then again I don't know.

+3  A: 

Your solution should work fine. Just add attr_accessor :notified to the model, and add a hidden field for it in the form: form.hidden_field :notified. Then, in the controller action, if validations fail, set @comment.notified = true before re-displaying the view.

Edit: Yeah, with multiple comments it is probably easier to just do

@notified = true

in the controller else statement, and then add

<%= f.hidden_field :notified, :value => @notified %>

to the form partial. Then, each comment will have notified set to true and will skip that validation.

mckeed
I think this is definitely going to work out, so big thanks mckeed. But, how would you define :notifed? Should I create a new definition at the bottom of the model, sorta like a callback? If so, how should it be defined? Or, should it be a Proc.new up at the :unless statement? Sorry, but I'm still a little new to rails, and I'm not really sure how to define it. (Also, my comment controller accepts and array of comments, but I don't think that will really matter as I think I can just do @commentS.notified = true) Really appreciated your answer, thank you again mckeed.
GoodGets
The only thing you need to add to the model is `attr_accessor :notified`, that line defines the `notified` and `notified=` methods so you can do `:unless => :notified` and `@comment.notified = true`. I'm not sure what you mean about the array of comments. Does each have a `name` field, or is that a different model? If you post the controller method where you create them, I could help you further.
mckeed
Hey mckeed, thanks for getting back to me. Each comment has a name field, as well as a text field. I've edited my question to include my controller. It collects all the comments, assigns them to the current_user, then rejects any that don't pass validation. A simple if/else statement follows, like if @comments.empty?, the user gets a "success" message, else errors are marked and the form is redisplayed. It's in this else statement that I'm setting notified to true. However, I think my problem could lie in my 'view' so I included that as well. I really do appreciate the help. Thank you.
GoodGets
What up mckeed. I added your edits to my partial and my controller; however, I was still unable to get it to work. My model still has the attr_accessor :notified and the validates_presence_of :comment, :unless => :notified lines. I played around with a bit and tried using a hidden_field_tag (instead of f.hidden_field) so that the whole form would have a hidden field, not each comment. Still a no go. I then put the @notified = true first in my controller so that all would be set to true no matter what, still no dice. I'm thinking the problem is in the validates_presence_of line?
GoodGets
Is :notified showing up in the comment params? Also, have you tried checking whether the comments have notified=true after they are created (the second time)? Remember if you have attr_accessible in the model you have to add :notified to that.
mckeed
I thought for sure that was it. I wasn't able to mass assign :notified since I hadn't added it my attr_accessible (such a dumb mistake), but now the problem is that it creates the comment no matter what. Notified is being passed in as "notified" => "", so it is indeed nil. But the controller creates these comments even though I have validates_presence_of :comment, :unless => :notified. Do I need to define notified to only pass if "true" ? (I tried defining it, but I get a stack level too deep error) I'll keep trying and google-ing different things, but I'm a little befuddled now.
GoodGets
Got it!!! I created a new method and called it in the :unless => notified? --Then I defined notified? as @notified == "true", and this works. The user submits the form once, gets the notification error, then submits the form again unchanged, and all are created. Thank you for your help mckeed, I really do appreciate it. (side note: In some of my own experiments, I had just been checking if @notified == true, instead of "true". I now realize the quotes matter as it is a string that is passed in)
GoodGets