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
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
Skave R. 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 anYes. 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 K. wrote:
Skave R. 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 anYes. 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,
PhillipI tried RJS and :update, but it didn’t really workt. could you provide
me a small example?
Skave R. 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} %>
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
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 %>
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
Phillip K. wrote:
Skave R. 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
enddef now
render :update do |page|
page.replace_html :current_time, Time.now
end
end
endapp/views/time/index.html.erb
<%= link_to_remote ‘Current Time’, :url => {:controller => :time,
:action => :now} %>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:
and I want to add this snippet into it:
Lorem ipsum dolor sit amet...
is that possible?
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:and I want to add this snippet into it:
Hello @User.id
Lorem ipsum dolor sit amet...
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 %>
<%= “
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
Get your action to:
render :text => ‘Hello world’
In your page:
link_to_remote(‘Some Text’, :update => “my_div_id”, :url => {:action =>
“my_action”})
Make sure your page has a
2008/7/3 Skave R. [email protected]:
Which one is better making an RJS or using
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
I always prefer RJS , because it makes debugging easier and my
controller a bit less clustered.
Check the API about RJS you will get many examples and all other help
needed.
Web Blogger wrote:
Which one is better making an RJS or using
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
As I understand it, this is RJS, just not using an RJS template, such
as
edit.js.erb
I always prefer RJS , because it makes debugging easier
That, I believe, is a personal opinion. 90-some-odd percent of my
debugging happens in controllers, and I like to have related code handy.
I often make some decisions inside the render :update block, and it’s
easier for me to have it in the controller.
and my
controller a bit less clustered.
It depends on a lot of things. I often grow weary of bouncing around
from file to file, as I tend to use a lot of view partials. When I have
one or two lines of code to respond to an ajax call, it doesn’t bother
me to have it in the controller.
I know there are best practices, but at the end of the day, I have to
get work done and be happy doing it. No two programmers are exactly
alike, and one of the things I like about Rails is that it allows a
certain something-or-other to be done in a variety of ways, but still
with programmer happiness in mind.
Peace,
Phillip
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs