RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

That is great approach, thank for the link. Unfortunately, in my current project I have two databases, and some models are connects to second database, so this approach does not work.

Avatar

I have no issues compiling assets with my rvm setup on my current hosted env. Going thru these steps (using rbenv) I can't get the assets to compile. Really strange stacktrace


RAILS_ENV=production bundle exec rake assets:precompile --trace
** Invoke assets:precompile (first_time)
** Execute assets:precompile
/home/deployer/.rbenv/versions/1.9.3-p194/bin/ruby /home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets --trace
** Invoke assets:precompile:all (first_time)
** Execute assets:precompile:all
** Invoke assets:precompile:primary (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke environment (first_time)
** Execute environment
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:primary
rake aborted!
Command failed with status (): [/home/deployer/.rbenv/versions/1.9.3-p194/...]
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/file_utils.rb:53:in block in create_shell_runner'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/file_utils.rb:45:in
call'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/file_utils.rb:45:in sh'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/file_utils_ext.rb:39:in
sh'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/file_utils.rb:80:in ruby'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/file_utils_ext.rb:39:in
ruby'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/actionpack-3.2.8/lib/sprockets/assets.rake:12:in ruby_rake_task'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/actionpack-3.2.8/lib/sprockets/assets.rake:21:in
invoke_or_reboot_rake_task'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/actionpack-3.2.8/lib/sprockets/assets.rake:29:in block (2 levels) in <top (required)>'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:205:in
call'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:205:in block in execute'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:200:in
each'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:200:in execute'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:158:in
block in invoke_with_call_chain'
/home/deployer/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in mon_synchronize'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:151:in
invoke_with_call_chain'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:144:in invoke'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:116:in
invoke_task'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:94:in block (2 levels) in top_level'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:94:in
each'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:94:in block in top_level'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:133:in
standard_exception_handling'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:88:in top_level'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:66:in
block in run'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:133:in standard_exception_handling'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:63:in
run'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/bin/rake:33:in <top (required)>'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/bin/rake:23:in
load'
/home/deployer/apps/Merj-Web/shared/bundle/ruby/1.9.1/bin/rake:23:in `'
Tasks: TOP => assets:precompile

Avatar

I could be missing something, but for me, fresh_when does not catch changes made to files and update the last modified or etag (e.g., layout, view or partial changes). I can check the modified time of each file that the action depends on, but that seems inelegant and is very error prone. Does anyone have a good solution? Or am I missing something?

Avatar

We have created a gem that handles things on the database and is supposed to be used with things like activeadmin, or typus:
https://github.com/helabs/static_content

It is fairly easy to implement, and works just fine. Give it a try.

Avatar

Care to share your validation check?

Avatar

this is great screencast, I am trying to do something similar, however, when I add friend, I want it automatically add the other side of the friendship too, something like in facebook. any ideas what I should be adding to my friendship create action.? thanks!

Avatar

The issue will be that Rails tries to connect to the database even just for assets precompiling, and the login for it fails. Check your database.yml so that it works when run on the web server etc.

Avatar

Or going even further, move all that database stuff into the model.

/app/models/faq.rb
def self.sort_positions!(ids)
  ids.each_with_index do |id, index|
    update_all({ position: index + 1 }, { id: id })
  end
end
/app/controllers/sorts_faqs_controller.rb
class SortsFaqsController < ApplicationController
  def create
    User.sort_positions!(params[:list])
    render nothing: true
  end
end
Avatar

The deploying with JRuby pdf is also good.

Avatar

I use torquebox and it is great. I don't have to deploy memcached, resque, use cron, etc. All encapsulated in one system, JBOSS.

Avatar

You know you can delete your comments if you find out your question isn't helpful to the others anymore, right?

Avatar

If you, like me, had trouble converting the Coffeescripts to pure Javascripts, this site is very helpful:

http://cs2js.nodejitsu.com/

Avatar

Well, I think the deployment headache is worth it when you see the performance if offers over MRI as well as what torquebox gives you out of the box.

Avatar

I use torquebox and yes, the deployments are the worst. The whole site goes down for 30 seconds.

Avatar

I would say yes and to run it on Torquebox if it has any complexity.

Avatar

Thank You, I missed the episode 337, with base.rb in place it's working now

Avatar

look into using wabler to bundle the rails project into a war file for java app server:

https://github.com/jruby/jruby/wiki/JRubyOnRails#wiki-War_File_Deployment

Avatar

look into using wabler to bundle the rails project into a war file for java app server:

https://github.com/jruby/jruby/wiki/JRubyOnRails#wiki-War_File_Deployment

Avatar

I have followed the steps suggested in the video to save a csv file and while I am not getting an error, the file is not saved by the uploader. Are there any common causes for the file not being saved that I should look into?

Avatar

Devise has been great for me. I am now looking to start authenticating into the devise powered web app I have built from a Native iOS app. Any guidelines, resources or best practices on this sort of integration?

Avatar

Great stuff! I feel silly that i didn't try JRuby before.

But I have some questions:
* Is there compilation from JRuby to JVM bytecode going on over the scenes?
* And is there way to compile JRuby to Java code?

Avatar

One thing I would do different is move the sorting into a new controller class and put the sorting in its #create action. This way we can stay within the boundaries of the seven actions.

/app/controllers/sorts_faqs_controller.rb
class SortsFaqsController < ApplicationController                               
  def create                                                                    
    # same as FaqsController#sort shown in screencast                                                        
  end                                                                           
end
config/routes.rb
resource :sorts_faqs, only: :create                                             
app/views/faqs/index.html.erb
<ul id="faqs" data-update-url="<%= sorts_faqs_path %>">
<% @faqs.each do |faq| %>
  <!-- same as shown in screencast -->
<% end %>
</ul>
Avatar

Three years later, and most of the information is still top notch. Thank you, Ryan.

I just finished a paypal integration for my website based on Ryan's guidelines.
This is to help out anybody who will be doing the same three years later after the initial screencast.
Two important comments were made here. Jim quoted the following:
* The 'cmd' parameter must be "_s-xclick" in the non-encrypted form, but it must also be included in the encrypted blob, as "_xclick"
Another point is that to let IPN pass through, you will need in the controller:
skip_before_filter :verify_authenticity_token, :only => [:create]
Finally, I chose to do a postback verification for IPN rather than the shared secret technique adopted by Ryan.
If you need help with this, just email me: danielsz @ sdf dot org.

Avatar

Yes, it looks like CanCan can be implemented in around 100 lines of code when using Strong Parameters! < https://github.com/colinyoung/cancan_strong_parameters

Avatar

CanCan seems to have a big flaw by not handling mass assignment: https://github.com/ryanb/cancan/issues/571

There is this very interesting Gem that is basically around 100 lines of code using Strong Parameters: https://github.com/colinyoung/cancan_strong_parameters

Avatar

I can't make this work on an iPhone browser. Does anyone know why?
I have created a project exactly like in the video, it works on every browser on my computers, but it doesn't work on any iPhone browser.

Whenever I press the send button, it doesn't do anything, not even clean the textbox. It does update every other browsers I have open at the time, except the one in the iphone or any other iphones viewing the same page.

Avatar

Definitely... this functionality is brilliant and not much code and should absolutely be included in Rails out of the box.

Avatar

I got this error because my VPS server had installed Rails 3.2.8 when my app was running Rails 3.2.6.

Definitely check your Gemfile and your database.yml file (of course). The problem here is clearly stated---Rails is not communicating with your database specifically due to an adapter (aka gem)

Avatar

Adrian - thank you! this is exactly what I needed (I have a bit different routes).

and for me (starting with rails) verbose is an awesome feature :)

Avatar

Thank you so mach and it works for me.

Avatar

This is a great piece of knowlege, thanks!

I encountered one problem.
The dynamic form doesn't work when I added validates :question_id, presence: true to the Answer model class.

Form is not saving and returning error: Questions answers question can't be blank
This happens only in case when you try to add a new question and add a new answer to it.

Why is this happening?

Avatar

Hi there, anybody know how I can implement this to self referential model. I am creating a record and on a form view also creating any associations needed if exists. if it doesn't exist, I should be able to add new one like in this railscast. this is where I got stuck. can anybody please help. Thanks!

Avatar

Aha, I redid my benchmarks on a small server using jruby --server --fast -S puma -t 4:4 and now the server just blazes. I'm impressed.

Avatar

Does anyone have a copy of this gist? It is now deleted. I am running into the exact problem that Matthijs is talking about where a failed validation is changing the url.

Avatar

Is anyone using a JRuby server for their production sites just out of curiosity? I've seen some impressive benchmarks for JRuby servers like Puma (https://github.com/czarneckid/puma-benchmarking and http://puma.io/), but in my personal testing I usually found it to be around par with MRI Servers like Thin. I assume there's some additional configuration for JVM to achieve those results.

Avatar

Does anyone know how to grab just the first frame of an animated gif and save it as a png? I'm having trouble finding this on stackoverflow and blogs.

The goal is to allow the user to upload gifs, but to only use the first frame so that there's not motion all over the page.

Avatar

Looks like you're missing Ryan's set_default method. This is a custom method that Ryan created to avoid using Capistrano's built-in _cset method. It's located in base.rb.

If you've got base.rb in your recipes folder and it contains set_default, make sure you're loading it before all other recipes.

Edit: You can see what base.rb looks like in the show notes for episode 337 (Capistrano Recipes)

Avatar

The generator for creating decorators is now called just decorator. Thus, use rails g decorator user in place of rails g draper:decorator user

Avatar

In a production environment would it be possible to compile my rails app into java bytecode ? I.E. the production server would have no rails app source code.

Avatar

+1

I am using Grails and Guard to compile my assets using JRuby which I feel has a faster asset compilation time.

Avatar

While therubyrhino is awesome, if you have nodejs installed on your machine, ExecJS can use it instead. This works with JRuby as well, and makes asset compilation much faster in my experience.

Avatar

I have some problems regarding 'trinidad:setup' step.
You mentioned 'trinidad_init_services' gem, but after all it isn't used, right ?
When trying to run 'cap trinidad:setup' I got an error:

.rvm/gems/ruby-1.9.3-p194/gems/capistrano-2.12.0/lib/capistrano/configuration/variables.rb:122:inmethod_missing_with_variables': undefined method set_default' for #<Capistrano::Configuration:0x00000002002218> (NoMethodError)

Avatar

Yes it definitively worth waiting. Note that's there is torquebox casts : http://torquebox.org/podcasts/

Avatar

You don't need to add the user directly to sudoers. Just add it to the admin group.

sudo usermod -a -G admin deployer

Avatar

+1 for torquebox though I think it's a pretty big tool and should be reserve for big projects with private server