Rendering action into div

Skave Rat wrote:

Is there a way to render an action into a div?

i.e.: I click on a link_to_remote link, and the content of an action is put into an <div>

Yes. You're interested in RJS. You can do it a few ways. One is to provide the element to update in the call itself, which means you will just do a render in the controller. Another is to use render :update in the controller and specify which element gets updated. You can also use rjs partials so the updating code is not cluttering up your controller.

Peace, Phillip

Phillip Koebbe wrote:

Skave Rat wrote:

Is there a way to render an action into a div?

i.e.: I click on a link_to_remote link, and the content of an action is put into an <div>

Yes. You're interested in RJS. You can do it a few ways. One is to provide the element to update in the call itself, which means you will just do a render in the controller. Another is to use render :update in the controller and specify which element gets updated. You can also use rjs partials so the updating code is not cluttering up your controller.

Peace, Phillip

I tried RJS and :update, but it didn't really workt. could you provide me a small example?

Skave Rat wrote:

I tried RJS and :update, but it didn't really workt. could you provide me a small example?

Sure.

app/controllers/time_controller.rb

class TimeController < ApplicationController   def index   end

  def now     render :update do |page|       page.replace_html :current_time, Time.now     end   end end

app/views/time/index.html.erb

<%= link_to_remote 'Current Time', :url => {:controller => :time, :action => :now} %>

<div id="current_time"> </div>

Run script/server, navigate to http://localhost:3000/time, and click the Current Time link. You should see the current time appear right below the link. Click it again and you should see the time refresh.

If the code gets munged, I'll post again with something that will not be technically correct but will hopefully be useful.

Peace, Phillip

Phillip Koebbe wrote:

Skave Rat wrote:

I tried RJS and :update, but it didn't really workt. could you provide me a small example?

Sure.

app/controllers/time_controller.rb

class TimeController < ApplicationController   def index   end

  def now     render :update do |page|       page.replace_html :current_time, Time.now     end   end end

app/views/time/index.html.erb

<%= link_to_remote 'Current Time', :url => {:controller => :time, :action => :now} %>

<div id="current_time"> </div>

Run script/server, navigate to http://localhost:3000/time, and click the Current Time link. You should see the current time appear right below the link. Click it again and you should see the time refresh.

If the code gets munged, I'll post again with something that will not be technically correct but will hopefully be useful.

Peace, Phillip

well, that's nothing new to me. My problem is rendering a view or HTML template into a div. i.e.: you have following in your opened page: <div id="div1"></div>

and I want to add this snippet into it:

<h1>Hello @User.id</h1> <p>Lorem ipsum dolor sit amet...</p>

is that possible?

The other way to do it (specifying the element to update in the call) would be done like this:

app/controllers/time_controller.rb

def now   render :text => Time.now.to_s end

Note that render :text can also be render :inline. They both seem to have the same result, but I don't know if the implementation of one is better than the other.

app/views/time/index.html.erb

<%= link_to_remote 'Current Time', :url => {:controller => :time, :action => :now}, :update => :current_time %>

<div id="current_time"> </div>

I'm not really sure why, but I prefer the render :update method. You can also render partials:

render :partial => 'path/to/partial'

or

render :update do |page|   page.replace_html :element_to_update, :partial => 'path/to/partial' end

And of course, you can use all of the normal caveats of partials (:collection, :object, :locals, :layout => false, etc).

Peace, Phillip

well, that's nothing new to me.

Hm. Okay. Sorry I insulted your intelligence. You asked for a small example. I showed you one.

My problem is rendering a view or HTML template into a div. i.e.: you have following in your opened page: <div id="div1"></div>

and I want to add this snippet into it:

<h1>Hello @User.id</h1> <p>Lorem ipsum dolor sit amet...</p>

is that possible?

Yes. I posted another message that indicated you can use partials. As far as I know, you cannot use a standard view. It must be a partial. I might be wrong in that, though. The AJAX call will be the same (and you can pass parameters if you need to), and in the controller action, do whatever you need to, and render a partial.

I do this in a situation in which I'm editing a resource. Suppose I have a widget that I want to edit, I might have a form that looks like

<% @widgets.each do |widget| %> <%= widget.name %> link_to_remote '(Edit)', :controller => :widgets, :action => :edit, :id => widget.id %> <%= "<div id='widget_#{widget.id}'>" %> </div> <% end %>

Then in the controller

def edit   widget = Widget.find_by_id(params[:id])   render :update do |page|     page.replace_html "widget_#{widget.id}", :partial => 'widget', :object => widget   end end

Does that help?

Peace, Phillip

It helps me!!