Pass JavaScript variables to Rails controller 1
Google query doesn’t give much useful result on this topic. What we discuss here is if there is a value/function getParameters() in JavaScript returns "message = hello_world" (a URL query). How do you pass the JS variable into controller?
link_to_remote
Most common way is using link_to_remote which constructs an Ajax call, but the drawback is you have to handle the params[:message] using RJS template (or please let me know if I was wrong). Here is the example:
<%= link_to_remote "Create Post", :url => {:controller=>'posts', :action =>'new'}, :method => :post, :with =>"getParameters();" %>
As you see, the :with option will eventually become :with => "message=hello_world". A new parameter is accessible by calling params[:message]. But what if I only want to render another page?
capture block
Capture block captures a variable for the view which can be reused. The idea is to call the JavaScript inside capture block to pass it as a Rails variable in view. The @param variable is readable in the view.
<% @param = capture do %>
<%= javascript_tag "document.write(getParameter());" %>
<% end %>
link_to_function
Remember that we have already passed the JavaScript value to @param variable, it is used here. An inline redirect_to does the trick, that redirects to the new action and value is accessible by calling params[:message].
<%= link_to_function "Create Post" do |page|
page.redirect_to "/posts/new" + "?" + @param
end %>
I’m sure that you will have other ways of using these tricks in difference situation, but hope you get the idea and use them whenever is suitable.
采访280 Slides的开发人员
最近看到280 Slides的release, 第一感觉让人觉得惊讶。界面很像Mac的设计,有很多人叫它"Keynote on the web". Boucher, Tom Robinson和Francisco Tolmasky在采访中还介绍了一些设计思路和未来的计划。相比Google Doc看重在互动和共享, 280 Slides更注重用户的体验。
访谈里面谈到Objective-J,Obj-J运用JavaScript并把它变成面向对象的语言(就好像把C转成Obj-C). 和Obj-C相近, 写代码的时候会看到很多方括号([]). 浏览器读到.j文件的时候会处理这些文件,这样就以为着开发人员可以做任何的标准的JavaScript的效果。提到Obj-J是不是很难学的问题,如果做过是一个JavaScript或者Obj-C的开发人员很快就能学会使用新的语言,但是了解frameworks需要一些时间。
Obj-J的framework被命名为Cappuccino. 开发者用Cappuccino的时候不用担心DOM的object, 不用担心CSS, HTML只要主意界面的开发。同时给开发人员一种开发平台的感受,而不是只是写一些代码。说道这个framework的起源是从3人大学开始的,之后为了开发一个网络程序,需要一个强大的javascript支持,所以设计了自己的framework。
更另人惊讶的是,Objective J将会是开源的,所有的代码都将可以在objective-j.org下载的到(现在可能还不存在)。这样还有很多经常遇见的问题,比如open source license, source control system, 还有documentation等等。(not an easy job :P)



