RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Is there a way to disable friendly ID in development mode?

Avatar

Thank you Alain. I had nested resources with comments for both resources, and this worked for me.

for example:

ruby
resources :events do
  resources :comments
  resources :pictures do
    :comments
  end
end
ruby
def load_commentable
  if params[:picture_id]
    @commentable = Picture.find(params[:picture_id])
  elsif params[:event_id]
    @commentable = Event.find(params[:event_id])
  end
end

Order from deepest nested resource down.

Avatar

if you want to get down into the nitty gritty and really focus on the formatting - perhaps take a look at Latex for rails, it was exactly what I was looking for.

Avatar

index_by works a lot better after a group by query compard to group_by.

Avatar

In #360 Ryan used this:

ruby
class SessionsController < ApplicationController
  def create
    user = User.from_omniauth(env["omniauth.auth"])
    session[:user_id] = user.id
    redirect_to root_url
  end

  def destroy
    session[:user_id] = nil
    redirect_to root_url
  end
end

class User < ActiveRecord::Base
  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.name = auth.info.name
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)
      user.save!
    end
  end
end
Avatar
ruby
u.facebook.put_connection("me", "cinematron:review", movie: "http://samples.ogp.me/457069940972565")

should be

ruby
u.facebook.put_connections("me", "cinematron:review", movie: "http://samples.ogp.me/457069940972565")
Avatar

I seperate crop-related.css from application.css, then it works.

Avatar

After I setting enviorment from Development to Production, the jCrop seems not working correctly. It maybe the problem caused by precompiling and compressing. Any suggestions? Thx!

Avatar

Sorry, I paid not enough attention to controller file. Everything works!

Avatar

Hi guys, still have save url error with the latest Mercury version. Are there any solutions?

Avatar

Great episode, thank you Ryan!

Avatar

Hi,

Is there a way to use the same Facebook app for both production and development?

I mean, in FB app settings you can provide only one Site URL, which for development is usually http://localhost:3000/ and for production your actual URL where the application is hosted.

Of course you could create a separate "development facebook app" with a different APP ID and SECRET and localhost:3000 as login callback url, but it would be nice, if OmniAuth Facebook and the same fb app could handle both development and production.

Avatar

Make sure that:

  • you're running it in JRuby (sorry but it happens ;) )

  • you've called require 'java'

  • you've called java_import 'com.dockyard.PgArrayParser'

  • you've compiled the class using javac

  • the .class file, or a jar file that contains it, is in your classpath at runtime

  • if the class is in a class file, rather than in a .jar file, then it will need to be in a com/dockyard subdirectory of a directory in your classpath.

-- Keith

Avatar

Everything was great up till the end when you did that markdown erb hack.
I agree about tilt, it would make things much easier.

Avatar

Morris.js looks super simple to setup and use.
My line charts required logarithmic Y-axis. So I had to settle on the Google Visualization API with the Visualr Gem. However, over time, I found it somewhat rigid. Therefore, I had to go to HighCharts which can do just about everything that I need to do. As Ryan points out, it is a commercial product though.

Avatar

Usually, I stick with LaTeX and PGF/TikZ to produce PDF graphs.

This has some disadvantages, though:

  • you have to know how to write a TeX document (takes ~2 years of experience with TeX).
  • you have to figure out how TikZ wants to work.
  • you have to combine Rails and LaTeX (which also requires a ~1GB installation of a recent TeX distribution)
  • I won't mention the higher memory and CPU requirements when compiling the PDF file... ;-)
  • using TeX for PDF and Morris/g.raphaël/whatever for HTML of course means maintaining two interfaces for your data (have fun extracting that into a gem ;-))

It's more of an advanced task (not the Ruby/Rails part, but the LaTeX one), but if you value the high quality of TeX'ed documents, it's definitely the way you should go.

Using v8 on the other hand sounds far to nice to be true :-)

Avatar

Great episode another option is: Google Charts

Avatar

What would you recommend for charts in both html and a generated pdf? Ever tried one of these libs with v8 on the server?

Avatar

Ryan -

Nice presentation, and thanks for getting the word out about JRuby. I think it offers a lot of possibilities to supplement MRI Ruby.

Regarding using JRuby to write Swing apps, if anyone's interested in seeing a small but nontrivial JRuby Swing app, check out my Game of Life Viewer at [https://github.com/keithrbennett/life_game_viewer], blog article at [http://www.bbs-software.com/blog/2012/09/05/conways-game-of-life-viewer/].

Also, in many cases you can eliminate JRuby's JVM startup times by using Nailgun (blog article at [http://www.bbs-software.com/blog/2012/09/15/hello-nailgun-goodbye-jvm-startup-delays/).

And, for more information to supplement this JRuby screencast, you can check out [http://www.bbs-software.com/blog/2012/09/04/jruby-presentation-northern-virginia-ruby-user-group/].

I worked with Java Swing for several years, several years ago, and have some comments and suggestions for those who want to take it further than the basic example you provided:


In my experience it's better practice not to call setVisible(true) when the frame is instantiated (that is, inside the class definition), but instead make it visible after it has been created. There may be other initializations you want to perform between the creation of the frame and the displaying of it, especially if it's happening at startup time.


An alternate way of instantiating a JButton is to pass it an instance of an Action class. This offers a cleaner separation between the visual button object and the business logic it executes. In addition, a single Action instance can be shared by multiple UI components. This comes in handy when you want the action to be triggerable by both a button and a menu item, for example. The UI components set themselves up as a listener to the action so that changing the enabled state of the action enables or disables all UI objects that use it.


It's a little cleaner to use Ruby-like property setter notation in the class, but make sure to preface the property with self. or else Ruby (any Ruby, not just JRuby) will consider it a local variable initialization. For example:

ruby
self.visible = true

# instead of:

# visible = true

# or:

# setVisible(true)

Normally it's necessary to call pack() on a JFrame after you've added all the components to it so that the frame will lay out its components properly. I guess it's not necessary with a single button.


You mention that in Java you would normally use an anonymous function for the behavior of an action listener, but I think you meant an anonymous class.


Using the JOptionPane show methods by themselves in a script without running a full fledged Swing program is a really great idea that never occurred to me. Thanks!

Cheers,
Keith

Avatar

I am trying to add JRuby support to my C-extension gem, by writing a Java class and executing it when the gem is installed on JRuby.

NameError: cannot load Java class com.dockyard.PgArrayParser

Any suggestion?

thanks

Avatar

@folivi In case you never found your solution, or for posterity in case other people find this comment while searching for this same solution

Make sure authenticates_with_sorcery! comes after include Mongoid::Document

E.g.

ruby
class User
  include Mongoid::Document
  authenticates_with_sorcery!
end
Avatar

Another great video Ryan!

I would like to see something on the Russian doll technique that DHH often refers to too - perhaps with an update to the PJAX cast as well, as I think it's changed significantly since the episode that covered it.

Avatar

thanks a lot for your awesome railscasts!
you're a superhero :)

greets from austria

Avatar

Why put everything in a separate controller for API? Image a separate API registration controller and a standard web controller; changes are made to the web controller forgetting to make the same changes to the API controller.

This is the problem that the respond_to blocks are suppose to help solve.

Take a look at VersionCake ( https://github.com/bwillis/versioncake ); easily set the API version in the request header. The appropriate view is then rendered for the request (e.g. show.v1.json.jbuilder). If the requested version of the view doesn't exist, the next prior version is rendered ensuring backwards compatibility.

Avatar

I'm having a similar issue. unicorn:restart doesn't seem to be reloading the unicorn process at all. However nothing to do with the assets, the actual start time on the process doesn't change.

Avatar

Ryan -

Love your screencasts but just a tip. Im a newbie and it seems like almost every screencast leaves out one or two small little details that are taken for granted. For example, I tried to setup a nested form here following your tutorial exactly and I couldn't get the nested fields to show up. After much research I learned that I needed to build the nested attribute within the parent's controller NEW action. Once I did this, the fields showed up. You do not show this anywhere on your notes or anything and I think steps like these are taken for granted. My recommendation is that your show notes should be completely thorough and should be a complete working representation of the code to make it work. You go through the process of starting a new app in this video (which I think could be taken for granted), but you leave this part out (which should not). Anyways, thought this would help...

Avatar

Wouldn't it be a good move to make the validation method private?

Avatar

How expensive would this solution be if you have 100,000+ interactions a day on your site? Does this scale well?

Avatar

The memoization is per-request based. It's good when you want to call the same (heavy) methods many times in a request lifetime (for instance, current_user).

Avatar

Hey Ryan,
thanks for this great screencast.
I have only one problem:
Is there any way to write a scope or maybe a query with arel to get all friends, own and inverse, in one query?

Avatar

another thing that people might want to play around with is partial match. searching for 'jun' with the blog-after code will get

SELECT "articles".* FROM "articles" WHERE (to_tsvector('english', name) @@ to_tsquery('jun') or to_tsvector('english', content) @@ to_tsquery('jun')) ;
 id | author_id | name | content | published_at | created_at | updated_at 
----+-----------+------+---------+--------------+------------+------------
(0 rows)

where as the following will get Superman (content has the word june) and Robin (junior) as a result

SELECT "articles".* FROM "articles" WHERE (to_tsvector('english', name) @@ to_tsquery('jun:*') or to_tsvector('english', content) @@ to_tsquery('jun:*')) ;

also, there is an additional contrib, pg_trgm, that can help with partial searching:

http://www.postgresql.org/docs/9.1/interactive/pgtrgm.html

and can also do similarity.

like unaccent, pg_trgm is not built by default for a postgres install.

Avatar

http://www.postgresql.org/docs/9.1/interactive/contrib.html

"When building from the source distribution, these modules are not built automatically, unless you build the "world" target (see step 2)."

unaccent is not built by default for a postgres install. this might be the issue.

Avatar

Ryan, in the video you mention that the category_name method may not increase performance due to multiple reads from memcached however, couldn't this be combated through the use of memoization as you show in episode 137?

Avatar

Yes you can.

This pull request enabled Fnordmetric to run as a rack app.
https://github.com/paulasmuth/fnordmetric/issues/5

However, keep an eye in your Redis Store. Depending on the amounts of events you might end running out of space in your Redis REALLY QUICKLY.

I ate a 100MB Redis Store in just 3 days (Hight traffic website).

Avatar

Here is my password_resets controller code, in case this is still a problem:

def update
@user = User.find_by_password_reset_token!(params[:id])
@identity = Identity.find(@user.id)
if @user.password_reset_sent_at < 2.hours.ago
redirect_to new_password_reset_path, :alert => "Password ↵
reset has expired."
elsif @identity.update_attributes(params[:identity])
redirect_to root_url, :notice => "Password has been reset."
else
render :edit
end
end

Avatar

It is simply an ActiveRecord object caching gem using Rails.cache methods.

Avatar

This is a read through and write through plugin for ActiveRecord.
It can cache normal find query or has_one/belongs_to query, even find by other unique index column.

https://github.com/csdn-dev/second_level_cache

Avatar

Excellent episode Ryan. I really enjoy the fact that you're including deployment related information in your latest videos.

Do you know of a gem that is similar to dalli for redis caching (for persistence)?