Pass JavaScript variables to Rails controller 1

Posted by jay Mon, 13 Apr 2009 23:26:00 GMT

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.

"Fork Ruby" Summary (RubyConf 2008)

Posted by jay Wed, 31 Dec 2008 00:23:00 GMT

I like screencast and keynote, they are great ways of learning practically. But some people don’t have much time to watch or listen to all these. Or maybe you forgot some ideas were presented, but it’s difficult to select the part you want to find in the video – you end up with watching the video again sometimes. So here I typed in a short summary of the keynote. If you are still interested, it’s worth to watching the video. And thanks to David Thomas giving this talk.

Presenter: David Thomas
Background: Last RubyConf attended was in 2005, started working on Ruby back in 1999. He wrote books, talked at many events and went to a lot of conferences. HE recently went to many Rails conferences.

Why Fork Ruby


There are a lot of active project forking Ruby interpreter, e.g. JRuby, IronRuby, MacRuby, but they are not forking the Ruby language.

Problem with Release Time


1.0 -> 1.2 tookk 14 months -> 1.4 (8 months) -> 1.6 (13 months) -> 1.8 (48 months) -> 1.9 (54 months)

  • As you can see, bigger changes we make, the bigger challenges they are, more time required for next release.

  • The bigger changes make developer difficult to adopt to these new features.

  • Less people use the new release feature, release team will not be able to find problems.

Some (Potential) Projects Ideas

RubyLite

RubyLite is a light version of Ruby. The idea is that moving those listed less used features into Gem, developer install those gems only when they needed.


  • One of the things we can do with RubyLite is to lose some language features that we normally don’t use (e.g.):

    • %q delimiters

    • implicit string concatenation

    • alias

    • nested assignment

    • :: sign for method call
  • And other features we can get rid of: class variables, global variables, many combination of $ sign, unless/until, protected, proc
  • One encoding will make library much smaller, so how about support utf8.
  • Losing Built-in classes and modules: Complex, File::Stat, FileTest, Mutex, ObjectSpace, Process::Gid/Status/Sys/Uid, Rational, TreadGroup
  • Losing Built-in methods, a lot of methods are vibrations that produces the same result.

Parallel Ruby

Ruby already have the construction to produce parallelism: a, b = b, a

Ideally, Dave suggested that we could perform calculation using this parallel feature in separate threads or processes: a, b = calc1(x), calc2(y). He used some sample code of finding longest word in English, which demonstrated that a few lines code produced Map/Reduce.

Optionally-typed Ruby


Closure-based Ruby


This Is the Ruby with block, and it should be easy to create lambda. Suggestion is to remove {} sign for hash. Maybe we can express methods and classes in blocks.

Conclusion


There is nothing wrong with Ruby, “but that shouldn’t stop us from having fun”. The community should fork Ruby and experiment with it. If the result is positive, they will be put in main Ruby release. The Ruby release itself should not be part of the experiment.