RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

is there any examples of how to test this authentication using functional tests?

Avatar

Along the lines of what @Fred said 4 years ago, I've found plenty of explanations on how to setup the models for many-to-many associations, but I have no idea how to setup the corresponding view and controller.

Something like http://railscasts.com/episodes/196-nested-model-form-part-1 but for many-to-many relations would be very helpful.

Avatar

I have been trying to follow along with this example in a rails 3.0.8 site I'm currently working on. I successfully got the feedzirra gem installed and am able to manipulate feeds in the rails irb console. But I can't seem to get the view to render the information stored in the DB.

I'm new to rails, so may not be any fault of the instructions provide here. In fact from the console an attempt at a mock view work as expected:

FeedEntry.update_from_feed(feed)
for entry in FeedEntry.all
    puts entry.name
end

runs the db query and delivers the the list of entry names. I have run

FeedEntry.update_from_feed(feed)

from the console and fired up webrick but everything I've tried in the view results in the same sorry condition; nothing to see. Am I missing something about how webrick is working? How the db is called? Any insights will be appreciated.

Avatar

When I go to the resque interface, i don

Avatar

I found that I still wanted my users to be able to update their username and email address. Because using user.authentications allows a user to be created without a password devise will complain when prompting to change the info as it requires a current_password.
I used this to allow someone that is logged in via an external auth provider to update without entering a password. You could disable the need for the current_password completely if desired.

First time posting... huge fan - I started with episode 1 and have learned a lot along the way. Thank you Ryan!

views/registrations/edit.html.erb

erb
<% if @user.password_required? %>
.... password fields ...
<% end %>

user.rb

ruby
  def update_with_password(params={})
    unless self.authentications.empty?
      params.delete(:current_password)
      self.update_without_password(params)
    end
    super
  end
Avatar

How would you recommend installing Redis on Ubuntu server?

Avatar

great screencast as usual.

here is my question.
is it possible to give every viewpath custom assets? say i want to create different themes for every shop or every blog in my application. every theme would have different assets (js, css, images) and different layouts and would inherit default behaviour from /views and /public. currently i am using themes_for_rails gem which provides this feature, but a build in solution in rails 3.1 would be great.

Avatar

I figured it out:

ruby
  constraints subdomain: /.+/ do
    scope :module => "sites" do
      root :to => 'public#welcome'
    end
  end

  root :to => 'public#welcome'
Avatar

Thanks for very good introduction to Papertrail.

I had a query regarding the "undo" link. What will happen if multiple users are performing updates on various products, and some of them starts undoing their updates. Wouldn't the sample app in your demo always undo the last update on the product table, and hence, many users will never be able to really undo?

Regards,
Srikanth

Avatar

Yeah, I'm doing that right now, but it seems like it will become tedious when I have a lot of controllers. Also, how would I define a resource, say "Post", where the controller is at controllers/sites/posts_controller.rb ?

I think I may have found my answer here actually:
http://glacialis.postmodo.com/posts/cname-and-subdomain-routing-in-rails
...about to try it out now.

Avatar
ruby
root to: "public#welcome"
constraints subdomain: /.+/ do
  root to: "sites/public#welcome"
end

http://guides.rubyonrails.org/routing.html#specifying-constraints

Avatar

I'm trying to store my I18n using redis for example :

  • locale: en
  • key: intro.label.title
  • value: "Hello World"

but my key change to introlabeltitle even my controller log like :
Parameters: {"commit"=>"Submit", "authenticity_token"=>"ZYoK5MMixIz9hiN8keOd9OTkKo6dBYluATYoyQDbmrY=", "utf8"=>"?", "value"=>"Hello World", "locale"=>"en", "key"=>"intro.label.title"}

and inside the redis-cli my key change to "en.intro\x01label\x01title"

here is my initializer :

TRANSLATION_STORE = Redis.new
I18n.backend = I18n::Backend::Chain.new(I18n::Backend::KeyValue.new(TRANSLATION_STORE), I18n.backend)

and my controller

I18n.backend.store_translations(params[:locale], {params[:key] => params[:value]}, :escape => false)

Thank you and sorry for my bad english

Avatar

What should you do when you have jobs that really should not be done by a web-app server (like resizing images and S3 uploading), but users are waiting for?

So while the backend benefits to be asynchronous, the user is actually waiting for the end-result.

Avatar

I thought the controller had to have a respond_to block to do the ajax or will it just default to to index.js.erb in this case?

Avatar

For those experiencing the same issue, just use UPPER before the input and searchable item. So, in the screencast example, it would be 'UPPER(name) LIKE UPPER(?)'

Avatar

Great screencast!

I'm trying to do something similar to the subdomain trick you just showed, but I want to change the controller that's used rather than the view. Is this possible?

I'm making a CMS where a user can create a site and enter their own subdomain. I'd like "/" to point to "public#welcome" if there's no subdomain, but if there is a subdomain, I want it to point to "sites/public#welcome".

Avatar

Based on the source code provided with this screencast (for the <%= yield(:head) %> in the app/views/layouts/application.html.erb ), update the app/views/snippets/show.html.erb to add:

ruby
<% content_for :head do %>
  <meta http-equiv="refresh" content="5"><!-- refresh every 5s -->
<% end %>

into the else branch (the one with <pre><%= @snippet.plain_code %></pre>) that displays the plain code when the highlighted version is not available.

If you wan't a progress bar, you certainly would have to use some javascript, and have the worker in charge of the background processing to update some attribute with its progress.

Avatar

Maybe the answer to this question is too obvious.In the video, when you submit the snippet, the page does not show you the colored code immediately but instead requires a refresh. For actual production site, how do you avoid having to refresh? Is there an easy way to implement a progress bar?

Avatar

How easy or difficult would it be to work this into a user account activation/verification email as well as password resets?

Avatar

Hi Ryan, great screencast as always.
Another solution would be to use an AMQP server (like RabbitMQ), it's way faster to fetch the queue item as it's not polling but pushing to the subscriber (i.e. worker). It's maybe less light than Redis though, but it does the job as well.
And you are not tied to load the entire Rails environment too.
Thanks a lot and keep up the good work !

Avatar

Is this buildin solution capable to replace and be a real alternative to authetication gems/plugins like authlogic?

Avatar

A good missing piece of this screencast would be an Ajax hook, so that the page would get automatically updated when the background worker completes it's job. (So one would not need to reload the page manually to see the highlighted snippet).

Best regards,
Ajasja

Avatar

Love the ep

As I don't use devise and HTTP Basic seems a bit weak for my uses I got the route to be conditional based on CanCan permissions:

http://www.arcath.net/post/Controlling_access_to_Resque_with_CanCan

Avatar

Love Resque and your screencasts as always.

I know this screencast is focused on Resque and not necessarily Rails best practices, but wouldn't it be more appropriate to have the enqueuing logic to an after_create callback on the Snippet class?

Avatar

will wait for jquerymobile railscasts!

Avatar

thanks for the tutorial.

i am already finishing the tutorial. but in the end i can't add a new answer or question by clicking new question or answer. and there is no error. any one can help me?

Avatar

Another great screencast from you, Ryan. Went through Resque's github Readme, saw the comparison between DJ and it, and got a couple of questions:

  1. For few background jobs each costing a few hours to run, which is preferable, DJ or Resque?

  2. Resque doesn't rely on Activerecord. It should work fine with MongoDB, right?

Avatar

Does anyone else think the app directory is getting too big to fast? I'm worried things are getting thrown in there. If you're using this technique, plus carrierwave, maybe some caching, presenters, and emails, your app directory would look like this:

assets
controllers
helpers
mailers
views
uploaders
models
sweepers
workers
presenters

Avatar

Does anyone know how Ryan gets SQL highlighting in the rails console? I have Wirble, but I only have that colorizing returned values (so if I do some_method.to_sql, I see the highlighting, but not when I do something like Model.find_all and Rails 3.1 shows the actual SQL right there in the console).

Anyone know the .irbrc trick here? Muchas gracias!

Avatar

I've just realized this is an sqlite issue, since on Heroku (Postgre) everything is working properly.

Avatar

Hi Ryan, have you ever considered doing a cast about Torquebox? Torquebox supports all Rack-based (J)Ruby frameworks and it comes with job scheduling and asynchronous task scheduling out of the box (no extra installs necessary) Torquebox is the most enterprise-oriented platform I've seen to date.

Avatar

Oops! Fixed. Thanks for reporting.

Avatar

Is there a way to update a nested form in a partial from the controller with an update page do call?

I build the Keyword form in a partial
Now I call a controller action from an observefield tag.
n.times { campaign.keywords.build}
page.replace_html "form
keyword", :partial => "keyword"

If I place the entre form in a partial, the call goes through, however, If I only have the keyword-part in the partial I can

Avatar

This fork of resque adds some nice features:

  • Vary the interval between polling queues
  • Skip queues under certain conditions
  • Expand queue name patterns into the real queue names
  • Specify the target queue when scheduling a job
Avatar

Must be me, but lib/tasks/resque.rake has the content of config/initializers/resque_auth.rb.

Avatar

@Ryan,

Does eventmachine serve the same purpose? And when will you use eventmachine?

Great information :)

Avatar

Great screencast as always Ryan!

You might want to consider writing tutorials for SmashingMagazine.com or net.tutsplus.com on How to create a rails app that does X. They pay really well when it someone who knows as much about their niche like you do :)

Avatar

fschwahn, see Resque, how to requeue failed jobs

Ryan, thanks, another clear and well structured episode.

Avatar

I guess it makes sense to take precautions if the webservice you are using is down.
Is it possible to automatically re-queue the job if it fails because it cannot reach the webservice?

Avatar

I'd like to use that Redis installation script:

bash
  $ cd /tmp
  $ git clone --depth=1 git://github.com/defunkt/resque.git
  $ cd resque
  $ rake redis:install dtach:install
  $ vim /etc/redis.conf
        "daemonize yes
         bind 127.0.0.1
         loglevel notice
         logfile /var/log/redis.log
         dir /home/app/public_html/application/shared/db_backup/" 
  $ cd /tmp
  $ wget https://gist.github.com/raw/892578/bf55748800e3ca812c5ad8233b933bd6283d3aff/redis.sh
  $ adduser --system --no-create-home --disabled-login --disabled-password --group redis
  $ mv /tmp/redis.sh /etc/init.d/redis
  $ chmod +x /etc/init.d/redis
  $ touch /var/log/redis.log
  $ chown redis:redis /var/log/redis.log
  $ update-rc.d -f redis defaults

  $ /etc/init.d/redis start
Avatar

Doesn't work man. I followed your guide from A-Z.

uninitialized constant User::BCrypt

But i have included the gem in the gemfile and also ran the bundle command.

Avatar

I'm getting /auth/failure?message=invalid_response although, when I inspect request.env['omniauth.auth'] I can see that it has received the auth info correctly?

It seems this very issue was discussed on Github - I've even upgraded to ruby-1.9.2 and am running Rails 3.0.9.

Sadly, no luck!

Avatar

I liked the article so much, thanks you.

I found a problem when I try to use uniqueness validation. It requests an ajax request http://localhost:3000/validators/uniqueness?case_sensitive=true&book%5Bname%5D=test but i receive 404 Not Found.

Should I implement this request myself from rails side?

Avatar

Any way to make it work better with UTF-8 characters? If I have "

Avatar

Any suggestions on how to get UTF-8 characters like "

Avatar

I found the solution, you must use ilike instead of like, cause heroku database is case sensitive:

ruby
@tracks = Track.where("nombre ilike ?", "%#{params[:q]}%")

I found this at this thread: Link

It worked ok for me, now I'm looking to make this work on my local development brunch.

Avatar

just use:

user1:
  email: john.doe@example.com
  password_digest: <%= BCrypt::Password.create(unencrypted_password)

I didn't test that, just writing from head but should do the job.

Avatar

I get the following error:

/Library/Ruby/Gems/1.8/gems/activesupport-3.0.9/lib/active_support/xml_mini/rexml.rb:20:in `parse': uninitialized constant ActiveSupport::XmlMini_REXML::StringIO (NameError)

This error propagates from the "gateway.authorize()" call. Any idea what's wrong with my setup? Thanks.