RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Ryan,

Do you have any tips on how to test OmniAuth with RSpec/Capybara?
I'm trying Link without success.

Thanks,

Avatar

Hi All,

Has anyone been able to figure out the "Validation failed: Password can't be blank, Password is too short (minimum is 6 characters" error when hitting Reset Password Submission button.

Please help???

Thanks in advance.

Avatar

I got the link to Sublime working for Windows 7. Here are the instructions: https://gist.github.com/Vaselinessa/5584662

It's as simple as creating a registry key and creating a batch file.

Avatar

Oh because I had to process the files on the server-side.

They are some source-code files, uploaded by users, which are fetched by the server for doing syntax highlighting. You are right, I should find some way to do the syntax highlighting only once and store that version on S3; and just give the link to the user.

Avatar

Thanks so much for this, Ryan!

I understand making gems Rails 4 ready is time-intensive, but most production apps I work on depend on gems that have yet to see some love. Nevertheless, we are starting the upgrade process and updating as dependencies are available.

Your cast makes the process very straightforward and manageable

Avatar

Thank you John for the snippet! It was really helpful for me.

Avatar

Rails has a method .httpdate, I think it's meant for this.

Avatar

Does anyone have any examples of using stripe in a nested form. For example I have a nested form that contains an affiliate which has_one subscription. This same affiliate also has a captcha. The problem is that that if i do something like

ruby
if @affiliate.subscription.save_with_payment && @affiliate.save_with_captcha

and there are errors with my affiliate model but not my subscription model, then the subscription gets saved without an affiliate_id, a customer is created by stripe and the same form re-renders populated with the stripe_token and the newly created subscription_id. Once the form is resubmitted and Affiliate.new(params[:affiliate] ) is called in the create action it is raising an error like Couldn't find Subscription with ID=31 for Affiliate with ID= since there is no affiliate_id in the subscription row in the DB. It would really be nice to see an example of this inside a nested form. Help!

Avatar

I'd suggest fairly strongly getting a nice deep familiarity with SSH and how it works.

There's a lot you can do between that and some sage knowledge on the sudo command to write a fairly well locked down server that's ready to rock the world.

Combine this with knowledge on OpenBSD and how it works and you have a tank of a server that's going to be near impossible to break into.

Recommended reading:

SSH Mastery: http://www.amazon.com/SSH-Mastery-OpenSSH-Tunnels-ebook/dp/B006ZO9ULK

Absolute OpenBSD: http://www.amazon.com/Absolute-OpenBSD-Practical-Paranoid-ebook/dp/B00CH96VB4/ref=sr_1_1?s=digital-text&ie=UTF8&qid=1368591922&sr=1-1&keywords=absolute+openbsd

Avatar

File chunking.

maxChunkSize: 1000000

I am finding that this directive is working for me. However S3 is not merging the file back together at their end.

I am only getting the last chunked bit of the file appearing in the folder.

I have found this

http://aws.amazon.com/articles/1109?_encoding=UTF8&jiveRedirect=1#14

But am a little unsure what to make of it. Was wondering if anyone else has come across this issue as it is pretty cool feature jquery upload. I am uploading video files and so being able to do it in batches means that their is less chance of file failure.

Hope someone can shed some light.

Kind regards,

Luke

Avatar

What tool do you use to see the number of queries done ? on the top left Windows ?

Avatar
ruby
= f.predicate_select only: Proc.new{|k| !(k =~ /_(any|all)$/) }

or in your case

ruby
= f.predicate_select only: Proc.new{|k| ['eq'].include?(k) }
Avatar

Still not getting around having to wrap your code into a cache block, but check out:
https://github.com/cpuguy83/pack_rat

It just lets you call:

ruby
def some_method
  cache do
    # stuff
  end
end
Avatar

cache_digests is for generating md5 digests of your fragments and inserting them into the cachekey for the object you are caching, this is for caching in the model itself.

Avatar

I'm rather new to rails and javascript/coffee
could some1 please explain to me the logic behind this line:

data: $('statistics_chart').data('statistics')

what is the magic so to speak, behind the scenes?

Avatar

I was looking for a good way to explain it, and found this. :)

def self.foo creates a class method.

"Class methods are methods that are called on a class and instance methods are methods that are called on an instance of a class."

via http://www.railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/

Avatar

Great cast but I noticed we had to write many methods.
When I checked identity_cache gem, I have also to write fetch_xxx

Is there any gem cache by default everything without need to change my old code to use fetch_xxx?
I need a gem to use without changing my code and have to replace all finds with fetches.

I found some gems like
https://github.com/ngmoco/cache-mone --not working on Rails3--
https://github.com/orslumen/record-cache
https://github.com/csdn-dev/second_level_cache

Can anyone give suggestions/recommendations?

Avatar

There is a typo in the ASCII cast:

rename title from: /app/views/templates/update.js.erb
to: /app/views/tasks/update.js.erb

Avatar

try also find_cache gem https://github.com/mustafaturan/find_cache
- support for rails 3+ including rails 4
- catches your find_by attribute methods
- catches your belongs_to relation
- catches your has_one relations
- threading supported

dynamically expires cache when thread killed or with an after_filter on application controller

Avatar

Another great cast.

I have a semi unrelated question being a bit of a rails newbie. I notice all the Model functions being defined as self.function_name

I have seen this done both with self and without self and also with variables being defined both with and without self. What is the right way? They both seem to work.

Avatar

If you are using the Dalli memcached gem, it is able to store binary data by using Rails.cache.fetch("foo", raw: true){Time.now}.

Btw, why do you want to cache it on server-side instead of delivering the S3 URL to the client? If the file is private, then you could use an authenticated URL with expiration

Avatar

I'm going to attempt to answer my own question, it makes a little bit of sense after having written it down.

The Rails.cache.fetch call will attempt to return something for the key passed in, but it you also pass it a block it will execute the block and use the resulting value for that key if it wasn't already set.

And passing [article, "foo"] as the cache key is the same as passing:

articles/5-2013blahblahblahfoo

which is just a unique identifier in the cache. Slowly filtering through my dense head.

OK great. So a better example might have used something that would demonstrably change had the block been executed each time the fetch message was sent:

Rails.cache.fetch([user,'foo']) { Time.now }
=> 2013-05-13 23:59:40 -0400

wait 5 seconds, then:

Rails.cache.fetch([user,'foo']) { Time.now }
=> 2013-05-13 23:59:40 -0400

Now I get it. ;-)

Avatar

2:34 to 3:03. Those two sentences makes no sense to me whatsoever. I've listened to it 10 times, and I can't tell what that line does. Sometimes the "foobar" terminology is unhelpful.

"So, if we pass a model object to the fetch method it's going to automatically use the cache key for that object, and we can supply multiple keys in an array and they will be joined together. So let's say we have some kind of property we want to compute on that article, we can execute that in a block here and this block will be executed just once when it sets the key and the next time we trigger it it won't trigger the block because the key is already set unless we change the Article model and that will change the key value."

Here is the line Ryan is describing:

Rails.cache.fetch([article, "foo"]) { 123 }

which returns:

=> 123

then he types it in the console again, and the same thing happens. What does this line do again? And what does "123" do? And how do I know that it isn't triggered the second time?

How is any fetching done when nothing in the cache had been set yet? I am really missing something on this one.

Avatar

When would you use this vs cache_digests?

Avatar

Wow Hilarious :)

I had a basic question. How can I cache static files that users have uploaded? I am using CarrierWave to upload the files which are hosted on S3, but every time I fetch them, it takes a very long time.

Avatar

Hi,
I've downloaded the example code and I am unable to get the remove and add links to work. The remove check_box works fine. Since the same code works on my friends machine I believe the problem may be related to the way rails is configured on my computer. When I click the remove link a get the following http://localhost:3000/surveys/1/edit#
(note the # sign after the edit).
Can anyone suggest possible reasons for this?

Sincerely,
Lisa

Avatar

remove in this order:
1. rails destroy simple_form:install
2. remove gem from Gemfile
3. bundle

Avatar

Awesome screencast! Thanks for exposing this, and making it easy for me to ask a question (almost a year later) "Gee, I wonder if this was ever discussed on Railscasts?"

After pulling my hair out a bit with EmberData, I'm completely blown away by meteor...

What's the word on meteor support for other database technology? Like a SQL db?

Avatar

Just needed to add the version in my Gemfile

before:
gem "active_model_serializers"

after:
gem "active_model_serializers", "~> 0.8.0"

Thanks

Avatar

For concurrent processing check Parallel gem also: https://github.com/grosser/parallel

Avatar

Thanks for this episode. Ryan's statement about not separating test code and test data makes sense. How about shared factories across tests? Is that a bad idea?

Avatar
ruby
commented on <%= link_to activity.trackable.recipe.name, activity.trackable.recipe %>

For this code, How come I get error undefined method, I'm not using the exact names for my values, I'm doing it this way,

ruby
commented on <%= link_to activity.trackable.list.name, activity.trackable %>

but I'm not sure why I don't have to add a .list at the end of my code? Instead I can just stop at .trackable

Avatar

I'm following along but getting some no hstore method erros when I run the migrations

rails g scaffold article name content:text published_on:date tags properties:hstore

and in the migration does this

execute "create extension hstore"
create_table :articles do |t|
t.string :name
t.text :content
t.date :published_on
t.string :tags, array: true
t.hstore :properties
t.timestamps
end

However, when I ran this migration I got an undefined method 'hstore' error. I then went into my test database and created the hstore extension as you see below (rather than in the migration file as Ryan did)

psql rails4test2_development
psql (9.2.3)
Type "help" for help.

rails4test2_development=# CREATE EXTENSION hstore;

But I got the same error when I ran the migration.

undefined method hstore' for #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::TableDefinition:0x007fa7ee7a9138>/Users/me/Sites/rails4test2/db/migrate/20130511204911_create_articles.rb:9:inblock in change'
/Users/me/Sites/rails4test2/db/migrate/20130511204911_create_articles.rb:4:in `change'

Can anyone help me understand why this isn't working for me?

Avatar

from the line above the line you show, allow is either true or is a block or is nil. The line you show then checks that (1) allowed is not nil and either (2a) allowed is true or (2b) allowed is a block which returns true when passed resource (and resource must not be nil)

Avatar

I suspect your test is so slow because of page.should_not have_content. Instead use page.should have_no_content

The first one will wait and see if the content appears. The second will test straight away that there is no content.

Avatar

i was getting this error

could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

and i solve it using host: localhost in the database.yml

Avatar

Thanks for making and maintaining this, Willem.

Avatar

RAWK does not appear to work with recent versions of rails.

After some digging around, found this newer and maintained gem called request-log-analyzer, which does very similar work. And I love the output formats that it produces. Highly recommended https://github.com/wvanbergen/request-log-analyzer

Avatar

Today I had some issues getting the progress bar to work as described by Ryan in this screencast. There seems to be an incompatibilty with jQuery 1.9 as Synthmax also mentioned.

In Firebug you'll see an this error when you try to upload a file:

Error: Syntax error, unrecognized expression:

I've found a solution to get the progress bar to work again.

Change the following line in your paintings.js.coffee:

data.context = $(tmpl("template-upload", file))

to:

data.context = $(tmpl("template-upload", file).trim()) if $('#template-upload').length > 0

Please let me know if this helps you