Forum: Ruby on Rails Using RJS in views without ajax

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.
F639e0cdf2fa098b535d6ec766b2aee1?d=identicon&s=25 Szymon Nowak (g0nzo)
on 2006-05-13 19:43
Hi!

I can use RJS in views like this (without using ajax):

<%=link_to_function("switch fields",
update_page do |page|
page.insert_html :before, 'date', date_select('exhibition', 'date')
page.remove 'date'
end) %>

Is there a way to move this RJS code into a function to separate file?
.js files are not parsed as rjs so it won't work. Can i just include
.rjs file like ordinary .js file?
6dab365a82517fb694650a57ee88e4a4?d=identicon&s=25 j`ey (Guest)
on 2006-05-13 20:10
szymek  wrote:
> Hi!
>
> I can use RJS in views like this (without using ajax):
>
> <%=link_to_function("switch fields",
> update_page do |page|
> page.insert_html :before, 'date', date_select('exhibition', 'date')
> page.remove 'date'
> end) %>
>
> Is there a way to move this RJS code into a function to separate file?
> .js files are not parsed as rjs so it won't work. Can i just include
> .rjs file like ordinary .js file?

You can include the action with the same name as the RJS, ie
<script type="text/javascript" src="/controller/action_with_rjs_file" />
That should work, I've done that before.

j`ey
http://www.eachmapinject.com
F639e0cdf2fa098b535d6ec766b2aee1?d=identicon&s=25 Szymon Nowak (g0nzo)
on 2006-05-14 14:14
Thanks.

But this way this action will be executed when the page is loaded. Can i
somehow execute this action on demand using link_to_function?

I could probably create helpers for rjs link_to_function calls, it won't
be perfect, but it should work and the code in views will be a bit
cleaner.
This topic is locked and can not be replied to.