RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

here's the code I used on the task to get it to work!(found it around github..)

ruby
task :install, roles: :web do
    run "#{sudo} add-apt-repository ppa:nginx/stable",:pty => true do |ch, stream, data|
      if data =~ /Press.\[ENTER\].to.continue/
        #prompt, and then send the response to the remote process
        ch.send_data(Capistrano::CLI.password_prompt("Press enter to continue:") + "\n")
      else
        #use the default handler for all other text
        Capistrano::Configuration.default_io_proc.call(ch,stream,data)
      end
    end

    run "#{sudo} apt-get -y update"
    run "#{sudo} apt-get -y install nginx"
  end
Avatar

Can you elaborate on why using attr_accessible as: :admin is a better approach than Ryan's approach?

Avatar

I know what's happened, I have to use the simpleform input types instead of rails default input types '^*^

Avatar

It appears that cap deploy:rollback doesn't rollback the precompiled assets at all. Does anyone have a recipe or solution?

Avatar

Great update for redcloth 2.0 and pygments. One quick comment though. I needed to add ".html_safe" after the "render(text)" method call. Not a biggie. Thanks!

Avatar

I'm newbie and follow the steps in this tutorial but validations not working (I can see the * for required fields but don't display any message). Using rails 3.2.3. Any idea?

this is the Gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.3'

Bundle edge Rails instead:

gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'

gem 'validates_existence', '~> 0.7.1'
gem 'simple_form'

Gems used only for assets and not required

in production environments by default.

group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platform => :ruby

gem 'uglifier', '>= 1.0.3'
gem 'therubyracer', :platform => :ruby
gem 'twitter-bootstrap-rails'
end

gem 'jquery-rails'

To use ActiveModel has_secure_password

gem 'bcrypt-ruby', '~> 3.0.0'

To use Jbuilder templates for JSON

gem 'jbuilder'

Use unicorn as the app server

gem 'unicorn'

Deploy with Capistrano

gem 'capistrano'

To use debugger

gem 'ruby-debug19', :require => 'ruby-debug'

Gems included by the bundle:
* actionmailer (3.2.3)
* actionpack (3.2.3)
* activemodel (3.2.3)
* activerecord (3.2.3)
* activeresource (3.2.3)
* activesupport (3.2.3)
* arel (3.0.2)
* builder (3.0.0)
* bundler (1.1.3)
* commonjs (0.2.6)
* erubis (2.7.0)
* execjs (1.3.1)
* hike (1.2.1)
* i18n (0.6.0)
* journey (1.0.3)
* jquery-rails (2.0.2)
* json (1.7.0)
* less (2.2.1)
* less-rails (2.2.2)
* libv8 (3.3.10.4)
* mail (2.4.4)
* mime-types (1.18)
* multi_json (1.3.4)
* polyglot (0.3.3)
* rack (1.4.1)
* rack-cache (1.2)
* rack-ssl (1.3.2)
* rack-test (0.6.1)
* rails (3.2.3)
* railties (3.2.3)
* rake (0.9.2.2)
* rdoc (3.12)
* sass (3.1.16)
* sass-rails (3.2.5)
* simple_form (2.0.1)
* sprockets (2.1.3)
* sqlite3 (1.3.6)
* therubyracer (0.10.1)
* thor (0.14.6)
* tilt (1.3.3)
* treetop (1.4.10)
* twitter-bootstrap-rails (2.0.6)
* tzinfo (0.3.33)
* uglifier (1.2.4)

Avatar

So one thing about using "store" is that it doesn't transparently work for the .to_json calls.

For example:
store :properties, accessors: [:blah]

If you do:
@things.to_json(:only => [:blah])
You'll get back
[{},{}]

If you do:
@things.to_json

You'll get:
[{properties: {blah: "foo"}}]

Which just isn't as convenient for merging in with your "standard" backbone.js or other JSON related infrastructure.

I'm not sure if there' a better way using to_json, but I'm guessing you can rescue the situation using say jbuilder or writing your own to_json call.

I'd be glad to hear better ideas though!

Avatar

I'm having a hard time finding more info on how to template a Controller. I could write a bunch of stuff to set the class name to reflect the generator name (e.g. rails g generator dumb_thing => Controller template with filename dumb_thing.rb and class name of DumbThingController) but this has to have been covered by Rails 3's generator/template support. Can you include a simple Controller in your rails cast next time please? Thanks!

Avatar

I did this recently. On first create rendered the pdf to webserver's filesystem (/public/generated_docs or similar). In future check for existence of the generated file and render a link to it rather than a link to generate the pdf.

If you don't have too many pdfs then you could pre-render all of them. Depends on your web app if this is suitable or not.

Avatar

Hey banhbaochay,

You should use the following instead.

ActiveRecord::Fixtures.create_fixtures("#{Rails.root}/db", "categories")

Stack Overflow

  • Phil
Avatar

Just as a follow up, I traced my problem to the user_id not being included in the join table, which was causing a validation failure. So I need to figure out how to pass the user_id to the join table (categorizations). I'm kind of stumped because the join table entries are being created behind the scenes, so to speak (i.e., I don't have an explicit statement saving those). Any help would be greatly appreciated.

Avatar

I'd like to see an example where categories and products are scoped through a current user. I'm trying to modify this example to scope things through a current_user and just can't seem to get it to work.
For example, in the form, I have:

ruby
<% current_user.categories.all.each do |category| %>

and in the controller, I have:

ruby
@product = current_user.products.new(params[:product])
if @product.save
etc...

The correct list of products is displayed on the form, but the save fails...

Avatar

YES! Thank You. I also have 3.1.2 and was not working for me.

Avatar

@djensen47 Your advice is still useful in Rails 3.2.3 and Ruby 1.9.3-p125 to make the published_on field show up the date. Thx!

Avatar

Will this gem work with associations? I'm looking to use a tableless model with nested forms. This gem appears to not be the answer. Roger mentioned the Modest Model gem above, but the readme states it is something the author would like to implement but theres not been any activity for 8 months on the project.

Avatar

I would like to note that if you want use image_tag in a mailer you have to set

ruby
config.action_mailer.asset_host = 'www.yourhost.com'

so that image_tags have the right source in the email

it will turn this

ruby
 # all images are broken
<img src="/images/icon.png" alt="Icon" />

into this

ruby
 # all images are fine
<img src="www.yourhost.com/images/icon.png" alt="Icon" />

since config.action_mailer.default_url_options has no effect on image_tag

Avatar

I am using it now with a few apps that are hosted on Heroku. But, I notice memory issues with those apps when I update the copy.

Avatar

Yes, just now!
and one similar issue is open on the Github page.

Avatar

Just a tip -- don't put it in your assets group in the gemfile or it won't work on production when deploying to Heroku.

Avatar

I'm on the third iteration of an alpha release for a system that, by 1.0, has been publicly committed to implementation as a service-component architecture. What I've got now is the result of a series of increasingly rushed three-steps-forward-two-back iterations, with the apps mostly breaking their faces against data formatting/communication over the wire.

The effort has been going on for some four months now. The first iteration used Grape around a couple of core components. Since we lost half our development staff, those components have been subsumed into the "traditional" Rails apps that now exist.

I just spent a couple of hours poking around with a greenfield prototype using RABL and got functionality working that took the then-larger team almost two weeks to get right.

I'll definitely be following the RABL project and making use of it in future. Thanks for the as-always excellent Railscast, Ryan! My Pro subscription is some of the best-spent money I can think of lately!

Avatar

Hopefully an easy question, but I haven't worked out the answer yet.

If someone knows my UID and the callback URL, what is to prevent them from skipping the actual authentication to log in as me? There must be some other piece of information in the request that lets me validate the source?

Avatar

Has anyone run into the problem that best_in_place editing works only once, and to work again one has to refresh the page?

Without refreshing the page, clicking on the field doesn't change it into an input field.

Avatar

Cool. Oracle also supports SELECT FOR UPDATE NOWAIT. It also has a feature named Database Change Notification that is similar to LISTEN/NOTIFY.

Avatar

+1 I really want to know how to use multiple view

Avatar

can't get it to work...

I'm not using a clean clone of this, but when installing nginx it will simply stop waiting the user to press [ENTER] and... well, nothing more..

** [out :: ec2-23-22-22-218.compute-1.amazonaws.com] Stable
** [out :: ec2-23-22-22-218.compute-1.amazonaws.com]
** [out :: ec2-23-22-22-218.compute-1.amazonaws.com] Stable version of nginx.
** [out :: ec2-23-22-22-218.compute-1.amazonaws.com]
** [out :: ec2-23-22-22-218.compute-1.amazonaws.com] More info: https://launchpad.net/~nginx/+archive/stable
** [out :: ec2-23-22-22-218.compute-1.amazonaws.com]
** [out :: ec2-23-22-22-218.compute-1.amazonaws.com] Press [ENTER] to continue or ctrl-c to cancel adding it

I tried getting dropbox through capistrano there too but it doesnt seem to be possible too because I'd need to stop dropboxd daemon after linking the accounts, doesnt seem to be possible...

the screencast was great... I`m not very confident in the approach of using it to set up the VPS tho... it seems anything that will provide a prompt that's not to input text will just stop the scripts forever

or I could be doing something wrong, were you guys able to get the cap deploy:install to work?

Avatar

did anyone have to restart the server before running nginx?

Avatar

Hey guys, two questions. First, can anyone point me to a good tutorial that would help with adding a 2nd application on the same server with a configuration like this. Second, lets say I wanted my app on Heroku to act as a staging app. Could you use a setup like this to pull from Heroku rather than Github?

Avatar

Is it possible to search an hstore by value instead of by key? For example, find all records for which value is 'red', regardless of key? The documentation doesn't say and this is likely a deliberate omission.

As an alternative, has anyone tried to build a key-value store using an association? That is, "product hasmany key_values", where key_values is a table with two indexed attributes, key and value.

Avatar

Heroku has released a Public Beta of their Shared Database at Postgres 9.1 - you just need to add it to your app through the Heroku command. See https://devcenter.heroku.com/articles/heroku-shared-postgresql if you're able to use beta versions it should work.

Avatar

I followed the example exactly at www.videos.org.in. However, styling works only with firefox and nothing happens in Chrome and IE. Assets are pre-compiled and are under public folder. Can some one help. Is there any config variable that I need to set?

Gemfile

source 'https://rubygems.org'

gem 'rails', '3.2.3'
gem 'sqlite3'

group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'therubyracer', :platform => :ruby
gem 'twitter-bootstrap-rails'
gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

Avatar

Confirmed, it was the postgres version, thanks for your help.
Ah but it's not going to work on Heroku until they upgrade from 8.3

Avatar

Do I really need an external git repository for this?

I'm not being able to find decent resources on a setup with only my machine and my vps...

Avatar

I use cap ryan:bates to solve all my problems :)
Thank you!

Avatar

No I've not yet tried phrase, what about the price?

Avatar

No I've not yet tried phrase, what about the price?

Avatar

Net::HTTP is part of std-lib which is installed with Ruby, you can find the documentation on ruby-doc.org. The links at the top of the page are for Ruby 1.9.3, if you're using an older version you'll have to scroll down to find the matching links.

std-lib will already be in your load path, so you can simply require files in it as you would files within your project:

ruby
require 'net/http'

What you need to require usually matches the name of the left frame in the std-lib documentation. Once you've installed a gem through Bundler you can require it in the same way, what you need to require differs per gem. Usually its the same as the gem's name, however there are exceptions (to be sure check out the documentation of the gems you're using).

Avatar
sudo apt-get build-dep ruby1.9.1

you can use apt-get build-dep to get dependency for building ruby!

Avatar

To fix this, make sure that in your application.css.scss you delete any '.css'.

Avatar

Any tips on getting hstore to work on Linode following these directions? I am seeing:

PG::Error: ERROR: could not open extension control file "/usr/share/postgresql/9.1/extension/hstore.control": No such file or directory

Avatar

This is probably a stupid question but is the Net::HTTP ruby library a gem or a library included in Ruby and how would I require it and the gems in my gemspec in my files?

Avatar

This a HashWithIndifferentAccess issue?

Avatar

Dude. My nomination to have you sainted or knighted or something!!! Sir Ryan Bates has a certain ring.

You've pretty much converted me from MySQL last week!

Avatar

I overcame the validation errors by adding :validate => false to the save statement in send_password_reset:

ruby
def send_password_reset
  generate_token(:password_reset_token)
  self.password_reset_sent_at = Time.zone.now
  save!(:validate => false)
  UserMailer.password_reset(self).deliver
end