Forum: Ruby on Rails Clilking on a link should replace that link with a form...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2063ef884e754d2667955c93da781833?d=identicon&s=25 Andrew (Guest)
on 2009-02-19 08:44
(Received via mailing list)
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)
<div id="new_journal">
    <%= link_to_remote 'New Journal',
      :url => { :action => :expand_create_form, :controller
=> :journals, :user_id => @user.id } %>
</div>

(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?
Ea52242ae25d195415cd9ef31db72aeb?d=identicon&s=25 Felix (Guest)
on 2009-02-19 09:36
(Received via mailing list)
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-partial...

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:

<div id="new_journal">
    <%= 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>
Ea52242ae25d195415cd9ef31db72aeb?d=identicon&s=25 Felix (Guest)
on 2009-02-19 11:32
(Received via mailing list)
Ha, just thought of a much better way:

(show.html.erb)
<div id="new_journal">
    <%= link_to_function 'New Journal' do |page|
        page[:new_journal].replace_html :partial => "create",  :object
=> @user
    end %>
</div>
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2009-02-20 02:47
(Received via mailing list)
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 Jones

On Feb 18, 5:38 pm, Andrew <registerons...@gmail.com> 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:
...
This topic is locked and can not be replied to.