Multiple view types for a single action?


#1

Is it possible to have more than one kind of view for a specific action,
for example an .rhtml and a .rjs file to handle the view for the same
action?

I suspect not, but this makes me wonder if there a way to call the .rjs
file from within the .rhtml so the statements within it get executed?

Thanks,
Andy


#2

Is it possible to have more than one kind of view for a specific action,
for example an .rhtml and a .rjs file to handle the view for the same
action?

I suspect not, but this makes me wonder if there a way to call the .rjs
file from within the .rhtml so the statements within it get executed?

http://api.rubyonrails.org/classes/ActionController/MimeResponds/InstanceMethods.html#M000062

Regards,
Rimantas

http://rimantas.com/


#3

Thank you Rimantas, I am sorry but should have clarified that I wanted
to be able to use the two views together for the single action, the page
you pointed me to answers the question the way I posted it but I should
have elaborated, apologies!

Basically, I want my .rhtml view file to be the display portion for the
given action but I also want it to be able to trigger the javascrpt
calls from a corresponding .rjs file, and this I am not quite sure how
to accomplish.

A quick of summary of what I want to accomplish is having do_stuff.rhtml
display the given page for an action ( do_stuff ) but also have
do_stuff.rjs contain javascript commands to show and hide different
elements on the page, I am not sure how to get to the .rjs file from the
.rhtml file.

Thanks!
Andy

Rimantas L. wrote:

Is it possible to have more than one kind of view for a specific action,
for example an .rhtml and a .rjs file to handle the view for the same
action?

I suspect not, but this makes me wonder if there a way to call the .rjs
file from within the .rhtml so the statements within it get executed?

http://api.rubyonrails.org/classes/ActionController/MimeResponds/InstanceMethods.html#M000062

Regards,
Rimantas

http://rimantas.com/


#4

<…>

A quick of summary of what I want to accomplish is having do_stuff.rhtml
display the given page for an action ( do_stuff ) but also have
do_stuff.rjs contain javascript commands to show and hide different
elements on the page, I am not sure how to get to the .rjs file from the
.rhtml file.

You may check render :update
http://api.rubyonrails.org/classes/ActionController/Base.html#M000206
(search for
“Rendering inline JavaScriptGenerator page updates”)
and
http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html

These shoul let you to achieve the effect you want. If you want to use
ready made .rjs
then I have no answer yet, and it would be interesting to find out if
there is a way to do it.

Regards,
Rimantas

http://rimantas.com/


#5

<…>

try { $(‘stuff1_div’).show(); $(‘stuff2_div’).show(); } catch (e) {
alert('RJS error …, etc

Any idea what I did wrong, I know page[‘div_id_here’].hide/show works
from inside an .rjs template so am confused why it is not working here.

You can use render :update in controller’s action which responds to
AJAX request.

If you want to have this update code in your normal view use
update_page_tag, like this:

<%= update_page_tag do |page|
page[‘stuff1_div’].show
page[‘stuff2_div’].show
end
%>

Regards,
Rimantas

http://rimantas.com/


#6

Thats terrific, if I could get that to work it would certainly be an
acceptable solution, but I get errors ->

I have the following div containers in my .rhtml page:

and added this inline javascript using the api as an example:

<%=
render :update do |page|
page[‘stuff1_div’].show
page[‘stuff2_div’].show
end
%>

which is giving me these errors:

try { $(‘stuff1_div’).show(); $(‘stuff2_div’).show(); } catch (e) {
alert('RJS error …, etc

Any idea what I did wrong, I know page[‘div_id_here’].hide/show works
from inside an .rjs template so am confused why it is not working here.

Thanks again!
Andy

Rimantas L. wrote:

<…>

A quick of summary of what I want to accomplish is having do_stuff.rhtml
display the given page for an action ( do_stuff ) but also have
do_stuff.rjs contain javascript commands to show and hide different
elements on the page, I am not sure how to get to the .rjs file from the
.rhtml file.

You may check render :update
http://api.rubyonrails.org/classes/ActionController/Base.html#M000206
(search for
“Rendering inline JavaScriptGenerator page updates”)
and
http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html

These shoul let you to achieve the effect you want. If you want to use
ready made .rjs
then I have no answer yet, and it would be interesting to find out if
there is a way to do it.

Regards,
Rimantas

http://rimantas.com/


#7

Thats awesome, exactly what I needed to know, thank you very much for
taking the time to illustrate this stuff! :slight_smile:

-Andy

Rimantas L. wrote:

<…>

try { $(‘stuff1_div’).show(); $(‘stuff2_div’).show(); } catch (e) {
alert('RJS error …, etc

Any idea what I did wrong, I know page[‘div_id_here’].hide/show works
from inside an .rjs template so am confused why it is not working here.

You can use render :update in controller’s action which responds to
AJAX request.

If you want to have this update code in your normal view use
update_page_tag, like this:

<%= update_page_tag do |page|
page[‘stuff1_div’].show
page[‘stuff2_div’].show
end
%>

Regards,
Rimantas

http://rimantas.com/