# FormTestHelper should change hidden fields

**URL:** <https://www.ruby-forum.com/t/formtesthelper-should-change-hidden-fields/85631>\
**Category:** Rails\
**Created:** [February 15, 2007, 12:44am UTC](https://www.ruby-forum.com/t/formtesthelper-should-change-hidden-fields/85631 "2007-02-15T00:44:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [February 15, 2007, 12:44am UTC](https://www.ruby-forum.com/t/formtesthelper-should-change-hidden-fields/85631/1 "2007-02-15T00:44:58Z")

</div>

Railsoids:

FormTestHelper rules. It tests your response.body form at the same  
time as it triggers your action, with all the parameters in that form.  
This obviously improves quality.

But it took issue with this:

```
submit_form 'validate_stuff' do |form|
  form['IBhidden'] = 'validate'
end

```

The field IBhidden is a hidden field. To be fair, FTH erred on the  
side of matching what a user can do. Users can’t change hidden fields,  
so FTH threw “TypeError: Can’t modify hidden field’s value”.

But that impairs FTH’s ability to match my JavaScript, which scribbles  
freely on that field. So instead of making a federal case out of this,  
I monkey-patched FTH:

module FormTestHelper  
class Hidden \< Field  
def value=(value)  
super(value)  
end  
end  
end

For FTH’s next version, who’s right? FTH, me, or both?

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [February 20, 2007, 9:23pm UTC](https://www.ruby-forum.com/t/formtesthelper-should-change-hidden-fields/85631/2 "2007-02-20T21:23:59Z")

</div>

Hi

Sorry for the late answer but I faced the same problem and found the  
solution so I guess I should share… You should call set\_value() method  
instead of =() like this

```
submit_form 'validate_stuff' do |form|
  form['IBhidden'].set_value 'validate'
end

```

Simon

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [February 20, 2007, 9:48pm UTC](https://www.ruby-forum.com/t/formtesthelper-should-change-hidden-fields/85631/3 "2007-02-20T21:48:01Z")

</div>

Simon P. wrote:

> ```
> submit_form 'validate_stuff' do |form|
> form['IBhidden'].set_value 'validate'
> end
> 
> ```

Righteous - I shall remove the monkey patch immediately.

(Dang that’s a hard habit to break!! Track the source to the point  
where you don’t like it, then rewrite that one spot! What could go  
wrong??😉

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [February 20, 2007, 11:27pm UTC](https://www.ruby-forum.com/t/formtesthelper-should-change-hidden-fields/85631/4 "2007-02-20T23:27:19Z")

</div>

I am looking for some advice about how to best take a string of RJS and  
convert it into a string of Javascript within a controller. I am  
presenting the user with a textbox and allowing them to type in RJS  
which will then be converted into Javascript and stored in the database.

Thank you,  
Matthew M.

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [February 21, 2007, 4:17am UTC](https://www.ruby-forum.com/t/formtesthelper-should-change-hidden-fields/85631/5 "2007-02-21T04:17:43Z")

</div>

Matthew M. wrote:

> The above code works just fine but I want a way to pass in arbitrary content between the do and end. Something like
> 
> @the\_rjs = render\_to\_string :update do |page|  
> “#{params[:rjs\_stuff]}”  
> end

page \<\< params[…]

> Where params[:rjs\_stuff] holds some RJS that was entered by a user. Yes I know this is a huge security risk but I am  
> prototyping something and this is what the project calls for at the moment. Regardless of what I put in for params[:rjs\_stuff] I get
> 
> try {
> 
> } catch (e) { alert(‘RJS error:\n\n’ + e.toString()); alert(‘’); throw e }

Install this as a plugin, or just raid it for source:

[http://phlip.svnrepository.com/svn/yar\_wiki](http://phlip.svnrepository.com/svn/yar_wiki)

The test file shows another way to extract RJS as a string, and shows  
how to turn off debug\_rjs:

def get\_rjs  
ActionView::Base.debug\_rjs = false

```
ActionView::Helpers::PrototypeHelper::
    JavaScriptGenerator.new(@controller) do |rjs|
  return rjs
end

```

end

Call rjs.to\_s before debug\_rjs becomes true again.

Here’s another system, from a private project that shamelessly stashes  
JS in the database:

def temporarily(obj, member, new\_value)

```
old_value = obj.send(member)
equator = member.to_s+'='

begin
  obj.send(equator, new_value)
  yield
ensure
  obj.send(equator, old_value)
end

```

end

def get\_js(rjs)  
temporarily ActionView::Base, :debug\_rjs, false do  
return rjs.to\_s  
end  
end

def javascript\_generator  
jsg = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator  
jsg.new(self){|rjs| return rjs }  
end

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [February 21, 2007, 3:59am UTC](https://www.ruby-forum.com/t/formtesthelper-should-change-hidden-fields/85631/6 "2007-02-21T03:59:42Z")

</div>

Matthew M. wrote:

> I am looking for some advice about how to best take a string of RJS and  
> convert it into a string of Javascript within a controller. I am  
> presenting the user with a textbox and allowing them to type in RJS  
> which will then be converted into Javascript and stored in the database.
> 
> Thank you,  
> Matthew M.
> 
> >

I have sort of figured out my question but need a little bit of advice  
to get past a roadblock.

@the\_rjs = render\_to\_string :update do |page|  
page.replace\_html ‘message\_top’, “Turtles are green”  
end

The above code works just fine but I want a way to pass in arbitrary  
content between the do and end. Something like

@the\_rjs = render\_to\_string :update do |page|  
“#{params[:rjs\_stuff]}”  
end

Where params[:rjs\_stuff] holds some RJS that was entered by a user. Yes  
I know this is a huge security risk but I am  
prototyping something and this is what the project calls for at the  
moment. Regardless of what I put in for params[:rjs\_stuff] I get

try {

} catch (e) { alert(‘RJS error:\n\n’ + e.toString()); alert(’’); throw e  
}

Any ideas?

Thank you,  
Matthew M.
