RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

You can change the current_user_method in the config/initializers/active_admin.rb file to use something else. I'm not sure what other dependencies Active Admin has on Devise though.

Avatar

any thoughts on choosing between activeadmin and railsadmin?

Avatar

Can I integrate with anything other than devise?

Does it works with other ORMs? (didn't have the time to test it yet)

Avatar

tailf doesn't exist in OS X, using tail -f -n 40 log/development.log works tho.

Avatar

Thanks for that tip. Just to clarify, you need to close this tag in following manner:

<%= BCrypt::Password.create("unencrypted_password")%>
Avatar

i am getting the following error when trying to add form for search functionality
TypeError in Locations#index

Showing c:/rails/maps/app/views/locations/index.html.erb where line #4 raised:

can't convert Symbol into Integer
Extracted source (around line #4):

1: All Locations
2: <%= form_tag locations_path, :method => :get do %>
3:
4: <%= text_field_tag :search, :params[:search] %>
5: <%= submit_tag "search near", :name => nil %>
6:
7: <% end %>
Rails.root: c:/rails/maps

Avatar

Hi, Ryan. Thanks for your wonderful episodes. I wonder if you can teach us how to use Textmate, I think it's simple but most important for developer, beacuse there are many tricks, plugins, etc.. and we don't know. Regards.

Avatar

@Lee Your post got me unstuck, thank you!

Avatar

Thanks for another great rails cast! over two years old and still great.

In case anyone else needs it, apparently the steps for getting credentials has changed. for up-to-date steps on getting the API access credentials, go here.

Avatar

respond_with is so sweet. It's odd that the generators with rails 3 still generate respond_to dos.

Avatar

Very interesting once again... though I read about alias_method_chain that in rails 3
> it has been replaced by a clever use of method overriding in modules and the super keyword.
on stackoverflow

Thanks a lot!

Avatar

I'm getting this too, I'm stuck!! Have compared my code with every file in the source code I can think of... Is it really working for everyone else following this screencast??

I'm running Rails 3.1.0, Ruby 1.9.2p290, sqlite3-ruby 1.3.3, Rake 0.8.7...

Avatar

Great episode as usual, but I'm getting sick of authentication. Not that I'm unappreciative, I do watch your episodes religiously each week.

Maybe an episode on Mercury, with carrier wave uploads and snippets for adding images to content from a gallery?

Avatar

Thanks Ryan for another great screencast! I really enjoyed and share the need for a very customizable authentication gem.

I was wondering if you could clarify this for me: I've seen the gem has an external submodule and all, but would you use Sorcery with OmniAuth or just stick to the external submodule? I really don't understand if these two gems overlap in this matter or are meant to be combined or hacked together :)

Avatar

Folks, there's one gotcha more I stumbled upon. I had formtastic forms in my app and after upgrading they weren't working anymore. Turned to be as simple as putting = in front of the tags. Rails 3.0 left (although deprecated) the output of <% (or - in haml) but Rails 3.1 seems more strict (which is a good thing) and won't output that!

Avatar

WARNING. As of Rails 3.1, one NEED to use = in front of semantic_form_for AND f.inputs, otherwise the tags won't be outputted. Took me some time to understand why my formtastic forms weren't working anymore!

Avatar

update: bundle update fixed binding.pry in tests

Avatar

This is actually a feature, you can use binding.pry to troubleshoot why tests fail, I even did:

ruby
class Test::Unit::TestCase
  alias_method :assert_block_without_pry, :assert_block

  def assert_block(message="assert_block failed.") # :yields: 
    begin
      assert_block_without_pry(message){ yield }
    rescue Exception => e
      binding.pry
      raise e
    end
  end
end

...but binding pry stopped working for me a week ago in tests, now it's throwing a NoMethodError: undefined methodactivate!' for nil:NilClass`

Avatar

After reading up on Rails sessions, I added

reset_session

to SessionsController#create

"One line of code will protect you from session fixation. The most effective countermeasure is to issue a new session identifier and declare the old one invalid after a successful login. That way, an attacker cannot use the fixed session identifier. This is a good countermeasure against session hijacking, as well."

http://guides.rubyonrails.org/security.html

Avatar

I am fairly new to rails, but I'd love to install and use this calendar, but how would you do that in rails 3?

the following command doesn't seem to work:

script/plugin install git://github.com/p8/table_builder.git

Thanks!

Avatar

I tried the code example but I get the following error when a request hits the server:
[2011-09-16 15:04:36] ERROR NoMethodError: undefined method new' for "ResponseTimer":String
C:/project/vendor/rails/actionpack/lib/action_controller/middleware_stack.rb:72:in
build'
C:/project/vendor/rails/actionpack/lib/action_controller/middleware_stack.rb:116:in build'
C:/project/vendor/rails/activesupport/lib/active_support/dependencies.rb:443:in
inject'

I am using Rails 2.3.5/ Ruby 1.8.7.

Avatar

Regarding validations...

Well, if you're like me, you have password validation and also encrypt the password before saving the user model.

That's a big monkey wrench for Ryan's excellent solution above.

To get it all to work, I created a PasswordReset model (the only way I know of to avoid the before save calls on the user model). I used almost all of Ryan's code but had to make a few modifications.

Then, I had to modify the password_resets controller to make use of the new model. I also moved the 2.hours_ago check to the edit action because it improves the user experience (oh, and I also plan on running a weekly job to remove old password reset tokens)

All-in-all it works pretty well (in need of some refactoring, though because I think the controller is too fat...) Here's the code - I hope it helps!

''' ruby

class PasswordResetsController < ApplicationController
def new
end

def create
user = User.find_by_email(params[:email])
if user
user.create_password_reset
UserMailer.delay.password_reset(user)
end
redirect_to root_url, :notice => "Email sent with password reset instructions."
end

def edit
password_reset = PasswordReset.find_by_password_reset_token(params[:id])
if password_reset.nil?
redirect_to root_path
return
else

@user = password_reset.user
if password_reset.created_at < 2.hours.ago
flash[:notice] = "Password reset has expired. Please try again"
redirect_to new_password_reset_path
end
end
end

def update
@user = PasswordReset.find_by_password_reset_token!(params[:id]).user
if @user.update_attributes(params[:user])
redirect_to root_path, :notice => "Password has been reset!"
else
render :edit
end
end
end
'''

Avatar

I'm also having trouble getting AJAX functioning in a Rails 3.1 environment. See my question on stackoverflow here.

Avatar

hi,
any ideas about a check-box multiple delete?

(I'm stuck with a routing error)

any help is welcome

Avatar

Does anyone know how to make sure the images loaded by jQueryUI library CSS are correctly referenced in the production?

I know from the one of the comments that you can use image-url('some_pic.jpg'); but that's simpler when you have written the code yourself, when you drop in a library from somewhere else, you have to hunt them all down the first time you use it and each time you upgrade it, that sounds real bad to me.

I am guessing this is a non-solvable problem at the moment. Because if it is solvable by some clever plugin, then what's the point of correcting all the url('dir1/dir2/image.jpg') to image-url('image.jpg')? But then including an external library that has images is not at all uncommon, I would think that the Rails team would have thought about it...

Avatar

I hate to be the negative one here, but this feature seems uncharacteristically over-engineered.

For example, it appears to require much more work to setup a Rails engine than before. You used to be able to simply add a plugin that included a config/routes.rb file, and those routes would automagically be mounted in your main application. Is that still the case? Is there any reason to go through all these steps if you're not making a shared gem?

Last point: the syntax of
mount Uhoh::Engine => "/failures"
seems backwards to me. I would have expected:

mount "/failures" => Uhoh::Engine
instead, which seems a lot more intuitive and consistent.

Avatar

Thanks Ryan, great screencast, I am using it in my app.

I have a users model with only an email and password_digest attributes.

One question (I really need help!): Once the user is logged in, how can I require the user to enter their current password in order to change/update their email or password?

Thanks!

Avatar

Thanks,
the community already fixed the token/code issue.
I added the view.

Avatar

Ok, I found the problem.

It's a confusion between :activation_code and :activation_token

@noam ben ari : I think you can update your wikis tutorial.

By default :activation_token and :activation_token_expires_at are created during the migration.

But after, in your tutorial you write user.activation_code instead of user.activation_token in *user_mailer.rb - activation_needed_email

Another little thing in Sorcery - Reset password tutorial, you forgot :

ruby
*# app/views/password_resets/edit.html.erb*
<h1>Forgot Password?</h1>
<%= render 'form' %>

Thank you very much for this amazing gem.

Avatar

did you miss the 'rails g controller sessions' by any chance?

Avatar

Hey Ryan,

Thanks for the great tutorials!

I've a question - I want an additional field (besides name, email) from the user for sign up that's not present in the 'auth' returned. So I'll have to redirect the user to a page with a form
to get the additional fields as inputs and then create the new user. Please let me know what's the best way to do this.

def create
auth = request.env["omniauth.auth"]
user = User.find_by_provider_and_uid(auth["provider"], auth["uid"])
if (user == nil) #user doesn't exist in database, create new user
user = # directs to a page with a form for additional fields and use some info from auth to create new user. how do i do this?
end
session[user_id] = user.id
redirect_to root_url, :notice => "Signed in!"
end

Thanks!
Aswath

Avatar

Need help for user_activation module.

I followed Sorcery's wikis tutorial but I don't know how to fix this problem. Everything works with these submodules :
:http_basic_auth, :remember_me, :reset_password

but with :user_activation :

NoMethodError (undefined methodactivation_code' for #User:0xacda7bc):
app/mailers/user_mailer.rb:22:in activation_needed_email'
app/controllers/users_controller.rb:29:in
create'`

Environnement : Rails 3.1.0 - Sorcery 0.6.1

Thanks.

Avatar

I created a Rails 3.1 gem called authbuttons-rails that adds authbuttons to the asset pipeline.

Avatar

Here is the error running under 3.1.0. Can not figure out why. Any suggestions? thanks.

=========================error===
wrong number of arguments (1 for 0)

Rails.root: D:/rails_proj/emclab-failed
Application Trace | Framework Trace | Full Trace

config/initializers/accessible_attributes.rb:7:in mass_assignment_authorizer'
app/controllers/categories_controller.rb:20:in
create'

Avatar

breaks in 3.1 and latest version seems to have problems compiling as it is looking for a Makefile, which Windows does not use.http://www.bagmulberry.com/mulberry-bayswater-bag-c-3.html

Avatar

Will this work in rails3 also?

Avatar

if you want controller specific scripts or stylesheets, you can do something like this:

ruby
javascript_include_tag params[:controller]
ruby
stylesheet_link_tag params[:controller]
Avatar

Same problem here. Have you figured this out?

Avatar

One of many thanks for your work. I however forgot to restart my server after making some of these changes... so took a while to figure my mistake. But, it works!

Avatar

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.

SEO Companies In Lahore

Avatar

That`s pretty awesome. Customizable authentication solution that i looking for. Thanks!

Avatar

filter_parameter_logging is deprecated in rails 3.1... instead put this in application.rb

config.filter_parameters << :card_number << :card_verification

Avatar

You're a complete rock star. I only wish you would use sign/log consistently. I know it's a small thing but I think people look up to you. Sign in/out/up (2 words) instead of login, log in, sign up, log out, etc.

Anyways, I learned a lot still. You're really an outstanding teacher and I watch your casts to get ideas about how to teach others.

Avatar

Authorization doesn't really depend on authentication. For example, I use Ryan's CanCan gem with simple http auth.

Avatar

Solved. I don't know why after copy-paste it did not work but after creating the same project from scratch everything worked fine. Thanks!

Avatar

Thank you for such a wonderful screencast. Personally, I still feel the way may Rails developer debug their code is still elementary. Although you may be using debugger or even a Pry, it is still obtrusive, meaning you have directly insert lines into your code like debugger or binding.pry which is a very inefficient way.

Rails developers should start switching to IDEs already.

Avatar

Can you recommend an authorization gem that works fine with sorcery?

Avatar

hi Ryan, I have done the same thing i.e dynamic select menus but using jquery. Please have a look here

http://rubylogix.blogspot.com/2011/08/dynamic-select-menus-in-rails-3.html

and you can find it on git-hub.

https://github.com/sandeepleo11/Dynamic-Select-Menus-in-Rails-3.

can u make a video of this. so that i would be happy.