RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Thank you @LucasCioffi, changing the location of my link_to_add definitely fixed the undefined method error!

Avatar

I had the same issue but your change gave me the error: can't convert Array into Hash

format.csv { send_data Product.to_csv } worked for me.

Avatar

Do you have any experience with combining multitenancy, with load balancing between replicated databases? Unfortunately Apartment doesn't play nice with a gem like Octopus which also patches ActiveRecord so they conflict ... I have been searching hi and low for a solution to this...

Avatar

I had the same error, but my problem was not the redirect, but rather my Amazon S3 bucket parameter was incorrect, i.e., ENV["AWS_S3_BUCKET"] was not set to the correct value.

Avatar

I had a "gotcha" with the S3Uploader class because my rails application always has a time zone set other than UTC and so my s3 request was failing with an invalid expiration time policy.

I just had to add the .utc to the end of this line in the S3Uploader intialize to get it working...

ruby
class S3Uploader
    def initialize(options)
      @options = options.reverse_merge(
        id: "fileupload",
        aws_access_key_id: ENV["AWS_ACCESS_KEY_ID"],
        aws_secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
        bucket: ENV["AWS_S3_BUCKET"],
        acl: "public-read",
        expiration: 10.hours.from_now.utc,  #<--- added .utc
        max_file_size: 500.megabytes,
        as: "file"
      )
    end
Avatar

In case anyone is curious on how this performs against Capybara-webkit as the driver, I found it to be ~10% faster. Additionally, the error messages are much more helpful.

Avatar

Capybara automatically waits for the content to appear. There's a default time it will wait that you can override. I'm not sure how you would reliably test for the "Fetching more orders.." part though. I'm betting that you would run into instances where that disappeared before Capybara could verify it was there.

Avatar

Where do I get the CONSUMER_KEY/CONSUMER_SECRET for omniauth-google?

Avatar

Hey guys, I just packaged this all into a gem called magiconf. It's super simple and easy to use. Based off of this + figaro. https://github.com/sethvargo/magiconf

Avatar

nm,

it was because of the non-restful reset link in the mail template.

Because I'm using multi-tenancy, and locales this is what my password_reset method must look like:

ruby
def password_reset(user, account)
  @user = user
  @account = account
  @reset_url = "https://#{account.subdomain}.notdienstapp.com/password_resets/#{user.password_reset_token}/edit"
  mail :to => user.email, :subject => "Password Reset"
end
Avatar

Now I have

ruby
def send_password_reset
    create_remember_token
    self.password_reset_sent_at = Time.zone.now
    save!
    UserMailer.password_reset(self).deliver
  end

 

    def create_remember_token
      self.remember_token = SecureRandom.urlsafe_base64
    end

and now I have problem with find user
undefined methodfind_by_create_remember_token!' for #Class:0x007fca97926210`

Avatar

I have an older project using Rails 2.3.14 and Ruby 1.8.7. Some of the routing constructs used here don't appear to be available in Rails 2. Any suggestions for how to implement a similar versioning system in Rails 2?

Avatar

I'm missing something trivial here.

ruby
No route matches {:action=>"edit", :controller=>"password_resets", :locale=>"uI1CmPo8Le5HoO6BTwtlzQ"}

but I want to see the reset_token parameter here don't I?

Avatar

Hi when I try to reset password I get this error:
ArgumentError in PasswordResetsController#create

wrong number of arguments (1 for 0)

app/models/user.rb:48:increate_remember_token'
app/models/user.rb:40:in send_password_reset'
app/controllers/password_resets_controller.rb:7:in
create'`

ruby
class PasswordResetsController < ApplicationController
  def new
  end

  def create
    user = User.find_by_email(params[:email])
    user.send_password_reset if user
    redirect_to root_url
  end

  def edit
    @user = User.find_by_password_reset_token!(params[:id])
  end

  def update
    @user = User.find_by_password_reset_token!(params[:id])
    if @user.password_reset_sent_at < 2.hours.ago
      redirect_to new_password_reset_path, :alert => "Password &crarr; 
        reset has expired."
    elsif @user.update_attributes(params[:user])
      redirect_to root_url, :notice => "Password has been reset."
    else
      render :edit
    end
end
end
ruby
class User < ActiveRecord::Base
  attr_accessible :email, :lname, :name, :password, :password_confirmation,
 
  has_secure_password

  before_save { |user| user.email = email.downcase }
  before_save :create_remember_token

  validates :name, presence: true, length: { maximum: 50 }
  validates :lname, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  validates :password, presence: true, :on => :create
  validates :password, length: { minimum: 6 }, :allow_blank => true
  validates :password_confirmation, presence: true, :on => :create



  def send_password_reset
    create_remember_token(:password_reset_token)
    self.password_reset_sent_at = Time.zone.now
    save!
    UserMailer.password_reset(self).deliver
  end

  private

    def create_remember_token
      self.remember_token = SecureRandom.urlsafe_base64
    end
end

Have anybody an Idea why?

Avatar

everytime i want to start "rackup faye.ru -s thin -E production" i only get the error
`require': cannot load such file -- faye (LoadError) but the gem is installed.. anybody an idea ?

Avatar

Thanks for this episode.
In the scrolling test case, I didn't catch how it works under the hood. As I understand it correct, the content "Order #11" is not present immediately after calling page.evaluate_script("window.scrollTo(0, document.height)").
Does this command wait for the content? If so, how should I test that message "Fetching more orders..." is showed (just for a while)?

Avatar

Once you start using more third party plugins, more advanced UI, own workflow and bindings, turbolinks may be a potential blocker. It can be useful, but only for simple sites, sites without too many legacy javascript code and plugins. So it is not better for every site, for sure.

Avatar

As of Bootstrap 2.2.1 the plugins listen to document instead of body, so modification shouldn't be necessary. You'll still need make other $(document).ready stuff happen on page:load as well though.

Avatar

Have anyone ever tried twitter-bootstrap-rails with select2-rails ?. It seems that the select2-rails is not working with the twitter bootstrap.

Avatar

Thanks for reporting that, I've updated the ASCIIcast to show the correct filename for the partial :)

Avatar

Thanks for this. Just what i was looking for. Works perfectly.

Avatar

Designing a site with turbo links in mind is a lot easier than making turbo links work on an existing site. In fact, it's a great tool to have in the rails stack by default. If I had to guess the rails team put it there because it genuinely makes every site better, and with little cost when designing a new app

Avatar

This is an unbelievably awesome platform, very interesting..

Avatar

Thank you for the railscast. I am having some problem redirecting the job to the resque-worker. I followed the similar steps as shown in the railscast. When I do

ruby
Resqueue.enqueue(SnippetHighlighter, @snippet.id)

I get the error that SnippetHighlighter is undefined in the rails server, not in the resque-web. I also have problem opening resque-web. It gives me strange error says:
/usr/lib/ruby/1.8/net/http.rb:560: in 'initialize': getaddrinfo: Name or service not known(SocketError).

Does anyone here know what is wrong.? I am using Ubuntu 12.04.

Avatar

Hi Ryan,
I followed this railscast and it helped me a lot in setting up a server recently however there is one weird issue I am encountering right now.

Whenever I enter my domain like www.example.com it gets redirected to the IP of the linode box having the rails application like xx.xxx.xx.xx. I have no idea why I am having this issue.I know it has something to do with Nginx configuration. Can you guide me what to modify to get this working ?

Avatar

In my create.js.erb, I have this:

$('.lkuptbody').append('<%= j render(@lookup) %>');

When I try to create a new lookup I get an error. The error says "Missing partial lookups/lookup." Didn't Ryan say that render is coming from the controller? Why is it looking for the partial instead?

Avatar

FWIW, I implemented a simple around_filter as shown here in my ApplicationController and it has massively slowed down my requests:

ruby
  around_filter :use_time_zone

  private

  def use_time_zone(&block)
    Time.use_zone('Eastern Time (US & Canada)', &block)
  end
Avatar

In the ASCIIcast when you move the condition fields into it's own partial. The header for that snippet still says:

/app/views/products/index.html.erb

But it should say:

/app/views/products/_condition_fields.html.erb

Thanks for an awesome episode,
Andy

Avatar

Just using
$(document.body).on
makes everything work as expected (if you need to add javascript files from an AJaX query)

Avatar

After playing a while with Turbolinks I had to disable it. Too many small problems with other JavaScript code. it also does not fit to more complex scenarios. I wonder why Rails core team decided to add Turbolinks to incoming Rails 4. For more complex site, all those additional side effects are not worth. I think, that full blown Javascript MVC framework (e.g. AngularJS) can be a better way. Not only faster (only chosen parts of the page are refreshed) but also simpler (no jQuery spaghetti hell, thanks to two-way data binding)

Avatar

Can someone explain more completely how this is different from PJAX? I hear everyone say "turbolinks is such a bad idea", although they offer nothing constructive. Is PJAX not a bad idea? I mean github uses it so it can't be that bad... If PJAX is fine, could they not implement it in the same way so it isn't "so bad".

Avatar

Well, you could add :dependent => :destroy to each has_many association. and then simply delete your Survey!

Avatar

I encountered this too!.. any solutions so far?

Avatar

Sweet.. I was looking for this... Thanks!

Avatar

Same problem, different solution
http://stackoverflow.com/questions/8177331/problems-with-guard-spork-rspec-rails-3

remove the test folder, which i guess result in test_unit not being used

Avatar

Running the scaffold generator without the nifty gem creates a field in the migration called 'create'

Avatar

I am also struggling with how to administrate a multi tenant app, and have not found much info about this elsewhere.

I'm not sure, but maybe creating an admin user:

  1. at the same time a new tenant/user is created
  2. and only if a admin user does not already exist

would suffice?

Not sure how to do this though.. searching...

Avatar

I would just make it a nested resource if you could and grab it off the params array like params[:establishment_id]

So you would be on page like:
http://example.com/establishment/123/category/new

And that would post to
http://example.com/establishment/123/category

And you could pull off params[:establishment_id]

Avatar

Look again. It went from 140 seconds to 80 seconds.

Avatar

This looks really great. I will test it. Its a lot less magic than turbolinks.

Avatar

Thanks, I was also asking myself the same question!

Avatar

That successfully removes column names that aren't created from foreign keys.
Let's say there's a product model that has many photos, then a has photos option will appear in the drop down, do you know how to remove it? Thank you.

Avatar

So how would you cache json? I think I am missing something obvious.

Avatar

Hello Ryan,

When I do the steps I still get a email can't be blank error message. Im trying to log in by username instead of email.