# DRY Javascript Degredation

**URL:** <https://www.ruby-forum.com/t/dry-javascript-degredation/98362>\
**Category:** Rails\
**Created:** [July 14, 2006, 3:51am UTC](https://www.ruby-forum.com/t/dry-javascript-degredation/98362 "2006-07-14T03:51:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamiequint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@jamiequint](https://www.ruby-forum.com/u/jamiequint)\
**Post date:** [July 14, 2006, 3:51am UTC](https://www.ruby-forum.com/t/dry-javascript-degredation/98362/1 "2006-07-14T03:51:13Z")

</div>

Hi,

It seems there must be a better way to do this than what I am  
currently doing.

I have a simple app that collects a users thoughts along with their  
email address and their first an last name. On a page that lists all  
the users thoughts they can click on a link to “add a thought” which  
then displays a form with ajax. My current methodology for this is  
not very dry. If js is not working it simply redirects to a “new”  
page that displays the same form as would have been displayed with ajax.

The problem is i have the entire code for this ajaxed form is in the  
view (rather than the non js “new” page which has the form code in  
“\_form”) I have it this way so I can do form\_remote\_for. I tried  
modifying “\_page” to only have the form setup code, but then it  
complains about nil objects because i am setting up the form\_for or  
form\_remote\_for in the view rather than in “\_page”. Is there some way  
to DRY this up easily or am I just destined to have my form code in  
two places.

~Jamie

---

<div class="post-metadata">

**Author:** ![jamiequint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@jamiequint](https://www.ruby-forum.com/u/jamiequint)\
**Post date:** [July 14, 2006, 3:57am UTC](https://www.ruby-forum.com/t/dry-javascript-degredation/98362/2 "2006-07-14T03:57:26Z")

</div>

Hi Jamie,

If the form is exactly the same in both cases then you should be able  
to have the form in only one place. Do you use the rjs templates? Do  
you know about respond\_to? I think these two together can dehydrate  
your app.

Peter

---

<div class="post-metadata">

**Author:** ![jamiequint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@jamiequint](https://www.ruby-forum.com/u/jamiequint)\
**Post date:** [July 14, 2006, 4:06am UTC](https://www.ruby-forum.com/t/dry-javascript-degredation/98362/3 "2006-07-14T04:06:42Z")

</div>

I’m using RJS templates yes,

my problem is i need form\_for for the regular requests and  
form\_remote\_for for the ajax requests, thus the form is not the same  
for both cases.

~jamie

---

<div class="post-metadata">

**Author:** ![jamiequint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@jamiequint](https://www.ruby-forum.com/u/jamiequint)\
**Post date:** [July 14, 2006, 4:47am UTC](https://www.ruby-forum.com/t/dry-javascript-degredation/98362/4 "2006-07-14T04:47:01Z")

</div>

Can you post a very simplified stripped down version of your wet code?

---

<div class="post-metadata">

**Author:** ![jamiequint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@jamiequint](https://www.ruby-forum.com/u/jamiequint)\
**Post date:** [July 14, 2006, 5:05am UTC](https://www.ruby-forum.com/t/dry-javascript-degredation/98362/5 "2006-07-14T05:05:49Z")

</div>

Sure…

in list.rhtml

### \<%= link\_to\_remote 'Add A Signature', :url =\> { :action =\> 'show\_signature\_form' } %\>

\<%= error\_messages\_for :signature %\>

 \<% remote\_form\_for :signature, @signature, :url =\> {:action =\> 'create'} do |form| %\> 

Email:  
 \<%= form.text\_field :email %\>

```
		.....more form setup here....

	  <%= submit_tag "Create" %>
	  <%= link_to_remote 'Cancel', :url => {:action =>

```

‘hide\_signature\_form’}%\>  
\<% end %\>

in new.rhtml (this file exists for javascript degredation purposes)

 \<% form\_for :signature, @signature, :url =\> {:action =\> 'create'} do |form| %\> 

Email:  
 \<%= form.text\_field :email %\>

```
		...more form setup here...

	  <%= submit_tag "Create" %>
	<% end %>
</div>

```

What I was attempting to say (may not have expressed it clearly) is  
that i tried to abstract everything between “form\_for” and “end” into  
a partial, however this doesn’t work because when the code is  
compiled it whines about nil stuff when it hits form.text\_field.

Notice the remote\_form\_for in the first one and just form\_for in the  
second one. This is where the problem lies, if i could just use one  
or the other i could throw the whole mess in a partial.

~Jamie

---

<div class="post-metadata">

**Author:** ![jamiequint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@jamiequint](https://www.ruby-forum.com/u/jamiequint)\
**Post date:** [July 14, 2006, 5:30am UTC](https://www.ruby-forum.com/t/dry-javascript-degredation/98362/6 "2006-07-14T05:30:48Z")

</div>

Hi Jamie,

Jamie Q. wrote:

> my problem is i need form\_for for the regular requests and form\_remote\_for  
> for the ajax requests

Don’t mean to seem like a smart ass, but…

Why?

You can use an RJS template pretty much anywhere you can use an .rhtml.  
What have you tried WRT using “form\_remote\_for” for the “regular”  
requests?

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![jamiequint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@jamiequint](https://www.ruby-forum.com/u/jamiequint)\
**Post date:** [July 14, 2006, 6:04am UTC](https://www.ruby-forum.com/t/dry-javascript-degredation/98362/7 "2006-07-14T06:04:25Z")

</div>

Well I guess I should have thought of that, I assumed that it would  
call create.rjs when in fact I wanted new.rhtml, however the proper  
controller method handled that fact, and now I have DRYed off.

Thanks!

---

<div class="post-metadata">

**Author:** ![jamiequint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@jamiequint](https://www.ruby-forum.com/u/jamiequint)\
**Post date:** [July 14, 2006, 6:26am UTC](https://www.ruby-forum.com/t/dry-javascript-degredation/98362/8 "2006-07-14T06:26:18Z")

</div>

On 7/13/06, Jamie Q. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What I was attempting to say (may not have expressed it clearly) is  
> that i tried to abstract everything between “form\_for” and “end” into  
> a partial, however this doesn’t work because when the code is  
> compiled it whines about nil stuff when it hits form.text\_field.
> 
> Notice the remote\_form\_for in the first one and just form\_for in the  
> second one. This is where the problem lies, if i could just use one  
> or the other i could throw the whole mess in a partial.

You know that remote\_form\_for can take an :html option to specify a  
“fallback” url for non-JavaScript browsers?

[http://api.rubyonrails.com/classes/ActionView/Helpers/PrototypeHelper.html#M000414](http://api.rubyonrails.com/classes/ActionView/Helpers/PrototypeHelper.html#M000414)

I’ve been specifying this fallback url for a situation I think is  
similar to yours.

Peter

---

<div class="post-metadata">

**Author:** ![jamiequint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@jamiequint](https://www.ruby-forum.com/u/jamiequint)\
**Post date:** [July 14, 2006, 6:51am UTC](https://www.ruby-forum.com/t/dry-javascript-degredation/98362/9 "2006-07-14T06:51:03Z")

</div>

What I ended up doing was using remote\_form\_for inside a partial  
called “\_form”

```
	<% remote_form_for :signature, @signature, :url => {:action =>

```

‘create’} do |form| %\>  
… form stuff…  
\<%= submit\_tag “Create” %\>  
\<% end %\>

Then in the controller doing this…

def create  
@signature = Signature.new(params[:signature])  
return if request.xhr?  
if @signature.save  
flash[:notice] = ‘Signature was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

so if it is a rjs request it returns and goes to create.rjs  
otherwise…well you can see.

voila! one controller action to handle both situations, nice and DRY.

~Jamie

---

<div class="post-metadata">

**Author:** ![jamiequint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@jamiequint](https://www.ruby-forum.com/u/jamiequint)\
**Post date:** [February 9, 2007, 3:27am UTC](https://www.ruby-forum.com/t/dry-javascript-degredation/98362/10 "2007-02-09T03:27:16Z")

</div>

Jamie, you are a genius!

I was having the same problem, after following the example from O’Reilly  
RJS pdf. The example they gave was for creating a new object, and does  
not work for editing an existing object, but the solution you provided  
works perfectly, thanks again!

Jamie Q. wrote:

> What I ended up doing was using remote\_form\_for inside a partial  
> called “\_form”
> 
> ```
> <% remote_form_for :signature, @signature, :url => {:action =>
> 
> ```
> 
> ‘create’} do |form| %\>  
> … form stuff…  
> \<%= submit\_tag “Create” %\>  
> \<% end %\>
> 
> Then in the controller doing this…
> 
> def create  
> @signature = Signature.new(params[:signature])  
> return if request.xhr?  
> if @signature.save  
> flash[:notice] = ‘Signature was successfully created.’  
> redirect\_to :action =\> ‘list’  
> else  
> render :action =\> ‘new’  
> end  
> end
> 
> so if it is a rjs request it returns and goes to create.rjs  
> otherwise…well you can see.
> 
> voila! one controller action to handle both situations, nice and DRY.
> 
> ~Jamie
