RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Can you post your PasswordResetsController? It seems that it has no "index" action.

Maybe you misspelled it, happens to me all the time :-S

Avatar

Hi,
This video is excellent. However I have a dumb question (a bit new to rails world). I am building a social network site. So I have 2 users User-1 and User-2, Now how do I stop/restrict User-1 from editing or accessing a User-2 Profile (even though User-1 is signed_in). Can you please help me on this. I do not want User-1 to have access to User-2 account. I can put a dirty code to do the checks inside the controller, but would like to know if Devise has any such pre-built functionality to restrict (just like if signed_in?)

Regards
Madhukar

Avatar

I think it's important to mention that when you set @current_user with cookie instead of session you lose the efficiency of rails CSRF protection.

It's happen because when rails protect_from_forgery method identify CSRF attack it's reset only the session and not the user's cookies.

To fix it you need to override handle_unverified_request()
at MODULE ActionController::RequestForgeryProtection::ClassMethods
and define it to delete auth_token cookie

Avatar

Hello

I'm having an issue with testing this using Rspec and capybara. I can get and post in Rspec which will set and pass session variables, but when I try and do Capybara request specs I cannot. The end result is that I cannot test any page that has before_filter authorize set on its action.

Thanks,
This is driving me crazy!

Avatar

Thanks for the plug, Linus! We're web developers at Websolr, and also felt the same pain of setting up and monitoring Solr servers for our client projects. Hence the birth of Websolr.

For those interested in trying out Websolr, you can use the coupon RAILSCAST278 at signup for your first month free of our Silver plan. (Or $25 off any other.)

Avatar

Real-time indexing is available in recent versions of Lucene, and can be accessed in Solr if you are willing to roll up your sleeves and write some Java. We're beta testing our own flavor of it over at Websolr

Avatar

Sphinx is faster for indexing, certainly, because Solr has a lot more overhead built in to that process. The 'client' software (Sunspot) has to fetch data from the database, format it into XML, then HTTP POST that XML back to Solr.

I like how Mat Brown (author of Sunspot) put it:

In my unbiased opinion, Solr is better than Sphinx in every way, except Sphinx is faster at reindexing the entire data set, which you pretty much never need to do. Unless you use Sphinx.

Avatar

There's some ongoing work to support the new, official Solr 3 spatial search APIs.

Avatar

You're looking for the highlight method:

ruby
Post.search do
  keywords params[:q] do
    highlight :title, :body
  end
end
Avatar

Regarding my above modifications, I needed "if password.present?" in the encrypt_password expression, otherwise it will end up encrypting an empty string when you save the user. You could also do this in a before_create callback or similar so it doesn't have to run on every single save.

Also, the find_by_email and authenticate methods need to be separated out. I changed it to:

...
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
...

Which I don't really like since I find relying on short circuit evaluation to be iffy.

Avatar

Hi I'm relatively new to rails and havvn't used a plugin before so just a little lost at the beginning.
I'm using Ruby on Rails and can't follow where does the bash code goes.
Help please

Avatar

This tutorial is so freaking good. I think I just messed my pants a little.

Avatar

It looks good, but it's not ready to use in nontrivial projects, you cannot use namescopes, something like this doesn't works

Product.published.near("Barcelona", 50, :order => :distance)

:(

Avatar

By the way, will_paginate has now been updated for Rails 3!

https://github.com/mislav/will_paginate

Avatar

No it's not a bug.

The only configs that are scoped to the engine are load_paths, eager_load_paths, and load_once_paths

Avatar

I couldn't get the ".local" shortcut to work in Lion. After some searching online (here: http://www.justincarmony.com/blog/2011/07/27/mac-os-x-lion-etc-hosts-bugs-and-dns-resolution/), it looks like the ".local" TLD is reserved by Bonjour and so weird things happen with those domains. Instead, per the articles's suggestion, I used ".dev" instead and everything works great!

Avatar

We're also have this problem when deploying with bundler. I found our problem on SO
http://stackoverflow.com/questions/6472785/bundler-error-on-deployment

Two of us worked on it for an hour and couldn't come up with any fixes.

Avatar

I would like to see this Railscast updated for Rails 3.1...

Avatar

It seems that configuration you place inside lib/uhoh/engine.rb will affect your main app's configuration at test/dummy/config/application.rb

I did this in my engine:

ruby
config.generators.do |g|
   g.template_engine :haml
end

And now the main app uses haml for scaffold generation. That seems pretty counterintuitive, since it is within the class of the engine; think that's a bug I should file on Rails?

Avatar

I was just playing around with this and noticed that the url helper prefixing seems to have changed. I am on Rails 3.1 rc5, but had to downgrade to Arel 2.1.4 to get things launched (which wouldn't seem to make a difference w/r/t this)

ruby
# undefined variable uhoh_engine
link_to  'Engine home', uhoh_engine.root_url

#works
link_to 'Engine home', uhoh.root_url
Avatar

I believe Rails 3.2.0.beta fixes the bug where it creates both app/views/layouts/application.html.erb and app/views/layouts/uhoh/application.html.erb (where "uhoh" represents any mountable engine name).

Avatar

I keep getting

ruby
Couldn't find User with auth_token = ):

in my template I use:

ruby
<% if current_user %>
                      Logged in as <%= current_user.email %>.
                      <% end %>

in my application.rb it is defines

ruby
def current_user
    @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token] 
  end

happens when a user is trying to reset the password.

Avatar

For anyone interested in a much more advanced sunspot search, you can check out my demo here.

Avatar

How do I test mountable engine with RSpec? If I follow instructions from episode #275, generators use Test:Unit instead of RSpec, even though I have rails-rspec gem installed.

Avatar

Great tutorial/example, like so many before :)

Is there any way (like already available through Sunspot or other gem/plugin) to "colorize" the results?

Example:
i search for term "sunspot"

There is bunch of results (title, content...), but every word in the result list that contains "sunspot" is lets say green or something.

Thx!

Avatar

Thanks for the great screencast.

A side note: I went through your github notes on how this website is configured and they were extremely useful to me.

I think it would be great to have a screencast showing how to configure a linode VPS for rails.

Avatar

Really great screencast.

I had a question. Am I right in thinking that you use request specs in favor of controller specs? Or, do you write controller specs too?

Avatar

I was wondering the same thing? I have thinking sphinx on my production server, but i think it takes a lot to always making sure its running...

Avatar

Rails framework was structured perfectly. Why did they move js css under assets? What is the main reason?

Avatar

Good screencast. Can you please explain why we need engine? Can I keep creating apps without engine or do I have to do it with?

Avatar

Oh boy. I overlooked a failed migration and this was solved by simply fixing my test database structure.

Avatar

As usual, excellent screencast Ryan. Have you built tests for authentication with has_secure_password? I'm encountering this error when creating a user in a functional test:

ruby
NoMethodError: undefined method `password_digest' for #<User:0x007faf810f8b00>
Avatar

how can a solr search be combined with an activerecord find_by_sql call?

Avatar

boosting a single attribute is simple

text :title, :boost => 2.0```
but how can you boost a block like this one?

text :composition_name do
composition.name
end
```

Avatar

You are right, it can be customized like this:

yaml
en:
  activerecord:
    attributes:
      user:
        password_digest: "Password"
Avatar

I think that the current Sunspot gem uses geohashing for spatial search which is inaccurate in certain scenarios.

Avatar

Another well timed Railscast. Had to look at Solr today as the last thing to implement on a project.

Thanks very much!

Avatar

Yes i'm facing problems with acts_as_tree while referencing another parent.

A has a child B then B becomes the child of C. It doesn't refresh the counter_cache for A and C, and i can't set it manually.

I had to force the update with :

Table.connection.update("UPDATE table SET children_count = #{old_parent['children_count'] -= 1} WHERE id = #{old_parent.id}")

If anyone knows any other solution i'm interested.

Avatar

facebook won't be around in 10 years, i need my own login

Avatar

From my experience, Sphinx is about 1000x faster than Solr when I have to index several millions rows from MySQL. And Sphinx can use case insensitive searching with different languages. I could not find Lucene collation files nor it was clear how to create them.

Avatar

I used a Sunspot with Mongoid like:

gem 'sunspot_mongoid'

ruby
#lib/tasks/sunspot.rake 
namespace :sunspot do
  namespace :solr do
    desc "indexes searchable models" 
    task :index => :environment do 
      #[list your models here].each {|model| Sunspot.index!(model.all)}
      [Article, Tag, User].each {|model| Sunspot.index(model.all)}
      Sunspot.commit
    end
  end
end
Avatar

I advise to pay attention to ElasticSearch and Tire. He also ??based on Lucene, supports real time indexing and easy scalability. But if you don't need it, then better use the Sunspot :)

Avatar

Hi,

I am a newbie to Rails/web programming, and am following along with your railscasts, which have been an awesome resource. However, I have come across a problem I can't seem to fix here, despite following the railscast. I get this error when running the test:

 Failure/Error: click_button "Reset Password"
 AbstractController::ActionNotFound:
   The action 'index' could not be found for PasswordResetsController
 # (eval):2:in `click_button'
 # ./spec/requests/password_resets_spec.rb:9:in `block (2 levels) in <top (required)>'

This, despite the fact that the create controller redirects to root_url, and when i do it through the browser, it works just fine. However, the test keeps wanting to route itself to the index action.

Anyone else get this error? Any ideas?

Avatar

Don't use model.image.to_s but instead model.image.url

Using to_s breaks the default_url override if you want to use a default image when none exists.

This guy:

ruby
  # Provide a default URL as a default if there hasn't been a file uploaded:
  def default_url
    "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  end
Avatar

Hi Ryan, there is something wrong with this video. It stretched and cropped, so there is nothing to see.
Are you still using fixtures? It's interesting is there still need in using them or what are cases when you would need them? :) Thanks

Avatar

I would be interested to see the differences or advantages of Sunspot with Solr to Sphinx with Thinking Sphinx.

I haven't used Sunspot/Solr but from this video it seems to be quite similar in features & implementation with TS.

Avatar

Just been 2 weeks old with using thinking_sphinx AND Now This looks more doable already:(
I guess I'll stick with thinking the sphinx for now... untill next project