# Clilking on a link should replace that link with a form

**URL:** <https://www.ruby-forum.com/t/clilking-on-a-link-should-replace-that-link-with-a-form/160742>\
**Category:** Rails\
**Created:** [February 19, 2009, 8:44am UTC](https://www.ruby-forum.com/t/clilking-on-a-link-should-replace-that-link-with-a-form/160742 "2009-02-19T08:44:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [February 19, 2009, 8:44am UTC](https://www.ruby-forum.com/t/clilking-on-a-link-should-replace-that-link-with-a-form/160742/1 "2009-02-19T08:44:58Z")

</div>

Hi everyone!

I’m new to Ruby and JS, and there is a problem I’m trying to solve:  
I need a link that, when clicked, transforms into a form for inputting  
data. I have next code:

(show.html.erb)

 \<%= link\_to\_remote 'New Journal', :url =\> { :action =\> :expand\_create\_form, :controller =\> :journals, :user\_id =\> @user.id } %\>

(expand\_create\_form.js.rjs)  
page[:new\_journal].replace\_html :partial =\> “create”,  
:object =\> @user

The problem is I don’t want to use the controller, is there some  
shortcut to call rjs code when user clicks on “New Journal” link?

---

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [February 19, 2009, 9:36am UTC](https://www.ruby-forum.com/t/clilking-on-a-link-should-replace-that-link-with-a-form/160742/2 "2009-02-19T09:36:04Z")

</div>

You could do something like this:

\<%= link\_to\_function ‘New Journal’, “Element.replace(this, #{ render  
(:partial =\> “create”,  
:object =\> @user).inspect})”%\>

check here for some gotchas with rendering partials this way:  
[http://tracesof.blogspot.com/2009/02/rails-partials-in-javascript-functions.html](http://tracesof.blogspot.com/2009/02/rails-partials-in-javascript-functions.html)

Maybe a much nicer solution in this case would be to render the form  
in a hidden div below the link and then just hide the link and un-hide  
the form when the link is clicked. Like so:

 \<%= link\_to\_function 'New Journal', "this.hide(); $ ('new\_journal\_form').show()" %\>

```
<div id="new_journal_form" style="display:none">
     <%= render :partial => "create", :object => @user %>
</div>

```

---

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [February 19, 2009, 11:32am UTC](https://www.ruby-forum.com/t/clilking-on-a-link-should-replace-that-link-with-a-form/160742/3 "2009-02-19T11:32:14Z")

</div>

Ha, just thought of a much better way:

(show.html.erb)

 \<%= link\_to\_function 'New Journal' do |page| page[:new\_journal].replace\_html :partial =\> "create", :object =\> @user end %\>

---

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [February 20, 2009, 2:47am UTC](https://www.ruby-forum.com/t/clilking-on-a-link-should-replace-that-link-with-a-form/160742/4 "2009-02-20T02:47:29Z")

</div>

Why are you opposed to using the controller?

In any case, you can drop the RJS into app/views/journals/  
expand\_create\_form.js.rjs  
and it should work. That _is_ the shortcut…

–Matt J.

On Feb 18, 5:38 pm, Andrew [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi everyone!
> 
> I’m new to Ruby and JS, and there is a problem I’m trying to solve:  
> I need a link that, when clicked, transforms into a form for inputting  
> data. I have next code:  
> …
