Forum: Ruby on Rails Multiple view types for a single action?

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.
Af1ee2208795752559b049502ded660c?d=identicon&s=25 Andrew Cowan (k2mia)
on 2006-04-07 15:13
(Received via mailing list)
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
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2006-04-07 15:22
(Received via mailing list)
> 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/ActionControlle...

Regards,
Rimantas
--
http://rimantas.com/
Af1ee2208795752559b049502ded660c?d=identicon&s=25 Andrew Cowan (k2mia)
on 2006-04-07 16:13
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 Liubertas 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/ActionControlle...
>
> Regards,
> Rimantas
> --
> http://rimantas.com/
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2006-04-07 17:04
(Received via mailing list)
<...>
> 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/ActionControlle...
(search for
"Rendering inline JavaScriptGenerator page updates")
and
http://api.rubyonrails.org/classes/ActionView/Help...

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/
Af1ee2208795752559b049502ded660c?d=identicon&s=25 Andrew Cowan (k2mia)
on 2006-04-07 17:32
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:

<div id="stuff1_div" style="display:none">
</div>

<div id="stuff2_div" style="display:none">
</div>


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 Liubertas 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/ActionControlle...
> (search for
> "Rendering inline JavaScriptGenerator page updates")
> and
> 
http://api.rubyonrails.org/classes/ActionView/Help...
>
> 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/
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2006-04-07 18:05
(Received via mailing list)
<...>
>
> 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/
Af1ee2208795752559b049502ded660c?d=identicon&s=25 Andrew Cowan (k2mia)
on 2006-04-07 18:49
Thats awesome, exactly what I needed to know, thank you very much for
taking the time to illustrate this stuff! :)

-Andy


Rimantas Liubertas 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/
This topic is locked and can not be replied to.