# Super() question

**URL:** <https://www.ruby-forum.com/t/super-question/88899>\
**Category:** Rails\
**Created:** [March 15, 2007, 12:55pm UTC](https://www.ruby-forum.com/t/super-question/88899 "2007-03-15T12:55:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pierre-Alexandre\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c4cdca/32.png) [@Pierre-Alexandre\_M](https://www.ruby-forum.com/u/Pierre-Alexandre_M)\
**Post date:** [March 15, 2007, 12:55pm UTC](https://www.ruby-forum.com/t/super-question/88899/1 "2007-03-15T12:55:02Z")

</div>

Hi folks,

I am trying to hack in\_place\_edit\_for to avoid empty fields:

def in\_place\_edit\_for(object, attribute, options = {})  
if params[:value] == ‘’  
params[:value] = ‘Hey! You must fill the field!’  
end  
super(object,attribute,options)  
end

It doesn’t work, the params[:value] is not affected.

But I’ve done the same thing with the in\_place\_editor helper to  
globalize options[] parameters and it works…

(ok, I could past the 3 lines of define\_method…)

–  
,========================.  
| Pierre-Alexandre M. |  
| email : removed\_email\_address@domain.invalid |  
`========================’

---

<div class="post-metadata">

**Author:** ![Pierre-Alexandre\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c4cdca/32.png) [@Pierre-Alexandre\_M](https://www.ruby-forum.com/u/Pierre-Alexandre_M)\
**Post date:** [March 15, 2007, 3:24pm UTC](https://www.ruby-forum.com/t/super-question/88899/2 "2007-03-15T15:24:28Z")

</div>

Are you sure params[:value] is actually an empty string? I’d shorten  
that to

params[:value] = “Hey! You must fill the field!” if  
params[:value].blank?

or, to avoid a string of spaces [correctly] validating as !blank?,

params[:value] = “Hey! You must fill the field!” if  
params[:value].strip.blank?

RSL

---

<div class="post-metadata">

**Author:** ![Pierre-Alexandre\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c4cdca/32.png) [@Pierre-Alexandre\_M](https://www.ruby-forum.com/u/Pierre-Alexandre_M)\
**Post date:** [March 15, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/super-question/88899/3 "2007-03-15T15:29:29Z")

</div>

On Thu, Mar 15, 2007 at 10:17:19AM -0400, Russell N. wrote :

> Are you sure params[:value] is actually an empty string?

params hash is:  
{“action”=\>“set\_user\_mail”, “id”=\>“3”, “value”=\>"",  
“controller”=\>“my\_bug\_app”}

> I’d shorten that to
> 
> params[:value] = “Hey! You must fill the field!” if params[:value].blank?
> 
> or, to avoid a string of spaces [correctly] validating as !blank?,
> 
> params[:value] = “Hey! You must fill the field!” if  
> params[:value].strip.blank?

You’re right, it’s cleaner 🙂 Anyway, it doesn’t work either :’(

–  
,========================.  
| Pierre-Alexandre M. |  
| email : removed\_email\_address@domain.invalid |  
`========================’

---

<div class="post-metadata">

**Author:** ![Pierre-Alexandre\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c4cdca/32.png) [@Pierre-Alexandre\_M](https://www.ruby-forum.com/u/Pierre-Alexandre_M)\
**Post date:** [March 15, 2007, 3:31pm UTC](https://www.ruby-forum.com/t/super-question/88899/4 "2007-03-15T15:31:29Z")

</div>

Well, it was worth a try. 🙂 I’ve never used in\_place\_editor so I really  
don’t know much about it. I was going to look at the API but its [gasp!]  
down. [again.]

RSL

---

<div class="post-metadata">

**Author:** ![Pierre-Alexandre\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c4cdca/32.png) [@Pierre-Alexandre\_M](https://www.ruby-forum.com/u/Pierre-Alexandre_M)\
**Post date:** [March 16, 2007, 4:08am UTC](https://www.ruby-forum.com/t/super-question/88899/5 "2007-03-16T04:08:16Z")

</div>

The reason it’s not work is because in\_place\_edit\_for defines an  
instance method which will access params at run-time. Not define  
time. So the statement you put before super will have no effect.

23: def in\_place\_edit\_for(object, attribute, options = {})  
24: define\_method(“set\_#{object}\_#{attribute}”) do  
25: @item =  
object.to\_s.camelize.constantize.find(params[:id])  
26: @item.update\_attribute(attribute, params[:value])  
27: render :text =\> @item.send(attribute)  
28: end  
29: end

You should monkey patch this method or you can simply define the  
action it would have defined yourself.

For some reason it has an options parameter but never uses it… so  
your patch could be flexible.

e.g.

in\_place\_edit\_for(:post, :comment, :check\_blanks =\> true, :blank\_error  
=\> ‘What do you want to say?’)

* * *

module ActionController  
module Macros  
module InPlaceEditing  
module ClassMethods  
def in\_place\_edit\_for(object, attribute, options = {})  
define\_method(“set\_#{object}\_#{attribute}”) do  
@item = object.to\_s.camelize.constantize.find(params[:id])  
if params[:value].blank? && options[:check\_blanks]  
render :text =\> options[:blank\_error] || “Fill in this  
field.”  
else  
@item.update\_attribute(attribute, params[:value])  
render :text =\> @item.send(attribute)  
end  
end  
end  
end  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![Pierre-Alexandre\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c4cdca/32.png) [@Pierre-Alexandre\_M](https://www.ruby-forum.com/u/Pierre-Alexandre_M)\
**Post date:** [March 16, 2007, 9:03am UTC](https://www.ruby-forum.com/t/super-question/88899/6 "2007-03-16T09:03:35Z")

</div>

On Fri, Mar 16, 2007 at 03:05:52AM -0000, eden li wrote :

> The reason it’s not work is because in\_place\_edit\_for defines an  
> instance method which will access params at run-time. Not define  
> time. So the statement you put before super will have no effect.

Gasp! You’re right. I’m so stupid.

> ```
> if params[:value].blank? && options[:check_blanks]
> 
> ```
> 
> end  
> end

Nice solution 🙂

Thanks.

–  
,========================.  
| Pierre-Alexandre M. |  
| email : removed\_email\_address@domain.invalid |  
`========================’
