RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

+1, thanks Ryan Bates. This is awesome

Avatar

How did you get the image on the railscasts.example@gmail.com? Address book or some gravatar-like service? :)

Avatar

How about passing an Array<#email> and add them all to the BCC?

You could also iterate over the array in a controller and deliver one at the time. I'd recommend using a service like SendGrid to handle this for you.

Avatar

+1 for a episode on Grape

Avatar

nvm I fixed it by adding resources:messages to my routes.rb file

Avatar

I keep getting an error saying

undefined method `new_message_path'

This is in app/views/messages/_messages.html.erb

for

<%= link_to "Reply", new_message_path(:parent_id => message) %>
<% if current_user?(message.user) %>
<%= link_to "delete", message, method: :delete,
confirm: "You sure?",
title: message.content %>
<% end %>

Any idea where to define new_message_path? I tried adding

def new_message_path
end

in app/controllers/message/controllers

but it didnt work

Avatar

Hi Ryan,

Thanks for all the great RailsCasts - I'm learning a ton of stuff!

I'm using your adding/deleting field partials fairly extensively and ran into a couple of things others may find useful:

  1. Your .gsub('\n','') removing newlines causes slight horizontal shifting of fields on inline forms. This was easily fixed by escaping rather than removing newlines using .gsub('\n','&#xA').

  2. jQuery won't recognize events from controls created this way. Apparently the solution is to use .on() (formerly .live and .delegate) though I have yet to try this.

Thanks again for all your great tutorials and keep up the good work.

Cheers,
Peter

Avatar

OK, I got it to get past this point, but now i'm getting the following error when trying to run the migration.

rake aborted!
database configuration does not specify adapter

Avatar

It's okay to start using the search_field_tag now. You'll get some benefits from mobile browsers and the correct styling in most desktops.

We also can start using phone_ and telephone_, number_, url_ and email_ helpers. On mobile device, for instance, these helpers queue the browser to pop up the correct virtual keyboard for the context.

Avatar

Problem in Ubuntu Server

If I run $ /etc/init.d/unicorn start, it runs just fine.
If I run $ service unicorn start, I get this error:

/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in 'require': cannot load such file -- bundler/setup (LoadError)
from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in 'require'
from /var/rails/mbiri/shs/bin/unicorn:14:in ''

I cannot figure out why /etc/init.d would work, but service would not. It's driving me crazy trying to find the answer. I also think this is the reason why unicorn won't start on boot, because I'v already ran update-rc.d

Avatar

I get the same error on multi-word search too aesthetica

Avatar

I cannot seem to get that to work under Ubuntu. Any ideas?

Avatar

Your correct, vagrant is not necessary. You can set everything up as usual. But where you might ask?

On server, I create the projects directory under /var/rails and somtimes suffixed with ./client-foo/project-bar

This is in tradition where apache sites are stored under /var/www.

So in other words, instead of:
/vagrant

use:
/var/rails
/var/rails/site
/var/rails/client-foo/project-bar

Avatar

In hindsight this was obvious, but dont apply any css width or height modifiers to the image the user is cropping. If you do this imagemagick will fail with "geometry does not contain image" since the coordinates you are giving it are not of the true image.

Avatar

Any thoughts on sending multiple e-mails such as a newsletter? A block perhaps?

Avatar

I had the same problems as Nelson, ran sudo on initdb and this was my repsonse:

initdb: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.

Not sure where to go from here. Did a TON of googeling with no resolution.

Avatar

This may be a classic "is the computer plugged in?" solution, but did you try running the command with sudo?

sudo initdb /user/local/var/postgres

This is probably a case where your /usr/local/var/postgres path (or a parent) doesn't have read/write permissions. That's fine, you don't want read/write access on those directories.

Avatar

If you really provision new servers that often, that you don't have time to reboot your chef-server, then have as many chef-servers as you need. I don't see why the preferred solution would be to exclusively use chef-solo. If you have some reason to prefer that approach, please do tell.

chef-server != SPOF

Avatar

If anyone still having a problem with saving the changes then add the following lines in your mercury.js file. Thanks to @seventhsense

ruby
  onload: function() {
    //Mercury.PageEditor.prototype.iframeSrc = function(url) { return '/testing'; }
    Mercury.on('ready', function() {
      var link = $('#mercury_iframe').contents().find('#edit_link');
      Mercury.saveUrl = link.data('save-url');
      link.hide();
    });

    Mercury.on('saved', function() {
      window.location.href = window.location.href.replace(/\/editor\//i, '/');
    });
  }
Avatar

Hi Paul,

I'm getting the same error, but after running the command you mentioned and retrying, i'm still getting it... the command ran properly as i see when trying to rerun it tells me it already has the latest version.

ruby
Reading package lists... Done
Building dependency tree       
Reading state information... Done
libmysqlclient-dev is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 42 not upgraded.

in my Gemfile i have:

gem 'mysql2'

and then the error when deploying:

failed: "sh -c 'cd /home/deployer/apps/sample_linode/releases/20120502195419 && bundle install --gemfile /home/deployer/apps/sample_linode/releases/20120502195419/Gemfile --path /home/deployer/apps/sample_linode/shared/bundle --deployment --quiet --without development test'" on 176.58.101.41

what do you think I'm doing wrong?

Avatar

How this would work when the models are associated via a has_many :through association?

I'm trying to follow the same steps, however nothing is displayed in my form.

I remember that the first episode regarding this topic used to build the objects in the controller, e.g. Question.new, so that they would be made available in the view. I found really weird that this wasn't mentioned this time.

Did that behavior changed in Rails?

In order to make it work with has_many :through I need to build "by hand" the associated object in my controller?

Thanks :)

Avatar

I want to resize / crop image from the center. Exactly like what twitter is doing? Is it possible to do the same via paperclip + ImageMagick? If yes how? If someone can help.

thanks

Avatar

@torrents I got same error, Any luck so far?

Avatar

Hi, have you found any solution to this issue?

Avatar

I did the follow steps and get this. I already put gem "bcrypt-ruby", :require => "bcrypt"
runned bundle install and restart my server.

Completed 500 Internal Server Error in 3ms

NameError (uninitialized constant User::Bcrypt):
app/models/user.rb:24:in encrypt_password'
app/controllers/users_controller.rb:8:in
create'

Rendered /home/hudson/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.8ms)
Rendered /home/hudson/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.9ms)
Rendered /home/hudson/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (5.3ms)

Avatar

Is it better to include bootstrap using the gem or by manually copying the css/js files yourself?

Avatar

Great screencast! However how can I pass a data set to json? Say in my controller index I have @products = Product.where(:status => "active") to initialize my view. In my view I've got a select tag to filter active, pending and inactive products. How can I pass the set of pending or inactive products to json?

Secondly, how do I properly link to an edit path in ProductsDatatable? It gives me a no method error if I do link_to("edit", edit_product_path(product)

Thanks!

Avatar

for the touch => true property in the association, is it possible to chain these together?

i.e.

class Article < ActiveRecord::Base
has_many :posts
end

class Post < ActiveRecord::Base
has_many :comments
belongs_to :posts, :touch => true
end

class Comment < ActiveRecord::Base
belongs_to :posts, :touch => true
end

in the above, if I do:
Comment.first.touch
will that touch the associated post AND article? (and therefore trigger a refresh on articles?)

Avatar

Hello!

May scripts from /etc/init.d/ be run by root at startup time?
If so, we have vulnerability allows "deployer" user change unicorn_init.sh and get root access, havent' we?

config/deploy.rb
sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
Avatar

Note: It appears that Nifty Generators are no longer viable with Rails 3.1+. I love the idea behind them of being so minimal and non-intrusive. I'm hoping Ryan has some time to give them some love, but in the meantime, be warned.

I posted a bug report on github for the 3.2 issue I noted, but it looks like the problems go back for quite some time.

Avatar

It works well in rails 3.2.3 with small change.
Be carefull , 'var saveURL' changed to 'var saveUrl' at the latest version of mercury.
Thanks.

Avatar

Thanks for for the screencast Ryan and thanks everyone for the helpful comments. I was able to deploy to AWS EC2 Ubuntu 11.10 (ami-baba68d3) using this script with a few minor changes.

A. Not sure if this is good practice but I used the default ubuntu user rather than creating a new user with admin rights. Couldn't get it to work with a new user, I think because AWS relies on keypair rather than password authentication for ssh. I suspect there is a better solution here, I'm not an expert on this.

B. In deploy.rb I used ssh_options to specify the location of my AWS keypair file:

ruby
server "ec2-23-20-247-239.compute-1.amazonaws.com", :web, :app, :db, primary: true
ssh_options[:keys] = [File.join(ENV['HOME'], '.ec2', 'ec2-keypair')] # Path to your aws keypair on your local machine

C. Added the solution from Breno Santos Salgado (above) in postgres, nginx and nodejs recipes to prevent the deployment getting stuck when the system asks you to "Press [enter] to continue".

D. Changed Ubuntu version number on line two of rbenv.rb for the version of Ubuntu I used:

ruby
set_default :rbenv_bootstrap, "bootstrap-ubuntu-11-10"
Avatar

Thanks, Ryan. Being able to see the email in the browser sure beats looking at the markup in the logs and imagining how it might look!

Avatar

How does chef compare to puppet?

Avatar

Hey Nick, thanks, this is useful to know!

Avatar

on Unix/Linux based systems remember that environment variables almost always refers to the operating systems is user/session/machine variables, not application configuration files.

Avatar
  1. ~/.profile file is quite safe.
  2. just remember to "export" to your environment like so: export GMAIL_USERNAME ='someone@gmail.com'

That should work. Works for me.

Avatar

I'm on Ubuntu. I thought the environment settings would be saved in a Ruby application config file. I guess I was wrong. I should just save this in ~/.profile? Is that safe? Thanks weblee and murdoch for the help.

Avatar

what Operating System are you using?

Avatar

Awesome tip ryan... I have been using cache in many applications, but never knew about this. Thanks!

Avatar

If you are using POW then you can setup a file called .powenv in the root of your app ie.

ruby
export GMAIL_USERNAME ='someone@gmail.com'

You can then use in your app like normal ie.

ruby
user_name = ENV['GMAIL_USERNAME']

DOCS: http://pow.cx/manual.html#section_2.2

Avatar

is there any way for selecting records like this from one model to add another model records. (both create and update)

Avatar

Can someone point me in the right direction on how to set up environment variables in rails?

Where for example would ENV["GMAIL_USERNAME"] = 'secret' be saved?

Avatar

When incorporating this code within my app, my list was not sorting until I added jquery-ui to my require list in the application.js manifest. Perhaps that is the same issue you are having?