RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

any idea how to create a separate class variable to each model inheriting from ActiveRecord::Base?

@@disable_validation will be the same object to all classes that inherit from ActiveRecord::Base, and I don't want that. =[ I need to create a specific counter to each Rails Model.

any tip?

Avatar

Ryan, your gotcha applies to the application.js manifest as well

And another gotcha: if you have 'will_paginate' in your Gemfile, it must be set to '3.0.pre2'

Thanks for another informative 'cast!

Avatar

I found that I needed to change this line:
<% form_tag projects_path, :method => 'get' do %>
to
<%= form_tag projects_path, :method => 'get' do %>
in order to get the form to appear.

Avatar

You could try @order.transactions.last.message

even better would be to pass the message back or store it directly in the order model so you save yourself a hit to the DB.

Avatar

That's perfectly normal. You have one spork instance for rspec and one for cucumber.

Avatar

Ryan,
Great screencast, and as typical a very timely topic.

Anyway, another way to get rspec to use spork instead of the

:cli => "--drb"

option is to put "--drb" in the .rspec file at the project root dir. I also include "--colour" and "--format progress" (or "--format nested") there.

Avatar

I'm having this problem too. Anyone has some tips?

Avatar

Added Cucumber and ran spork cucumber --bootstrap to ensure it bootstrapped env.rb, features are now running twice. In fact, launching guard loads the rails environment twice as well?

bash
Starting Spork for RSpec & Cucumber 
Spork server for RSpec & Cucumber successfully started
Guard::RSpec is running, with RSpec 2!
Running all features
Using RSpec
Using Cucumber
Preloading Rails environment
Preloading Rails environment
Disabling profiles...
Loading Spork.prefork block...
Loading Spork.prefork block...
Avatar

require 'development_mail_interceptor'

Avatar

Hello, I'm using an interceptor to the User when registering on my site it receives a confirmation e-mail and I also know that a User has been registered .. S'p that only I am getting the mail, the User does not receive ...

Can anyone help me?

app/controllers/contatos_controller.rb
view plaincopy to clipboardprint?
ContatoMailer.contato_confirmation(@contato).deliver

no meu
config/initializers/setup_mail.rb
view plaincopy to clipboardprint?
require 'development_mail_interceptor'

ActionMailer::Base.smtp_settings = {

:address => "smtp.gmail.com",

:port => 587,

:domain => "localhost:3000",

:user_name => "usuario",

:password => "secret",

:authentication => "plain",

:enable_starttls_auto => true

}

Mail.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development?

no meu
lib/development_mail_interceptor.rb
view plaincopy to clipboardprint?
class DevelopmentMailInterceptor

def self.delivering_email(message)

message.subject = "#{message.to} #{message.subject}"

message.to = "admin@admin.com"

end

end

no meu app/mailers/contato_mailer.rb
view plaincopy to clipboardprint?
class ContatoMailer < ActionMailer::Base

default :from => "admin@admin.com"

def contato_confirmation(contato)

@contato = contato

mail(:to => "#{contato.nome} <#{contato.email}>", :subject => "Confirma

Avatar

hi !

i got some js periodic js errors saying that the connexion has been interrupted while the page was loading every 20 30 seconds, whatever is the timeout.

Does anybody experience this kind of problem ?

Avatar

move
gem 'sass-rails', " ~> 3.1.0"

out of the :assets group.

assets load last, active admin needs sass to load sooner.

Avatar

True, the "default" Rails stack has grown a lot in the last couple of years. But previously we just didn't do this kind of advanced testing when hacking Rails. We loved the simplicity back then, but did also miss out on a lot of advanced features. Now that we have them we miss the innocent days ;)

Avatar

For testing I use rspec, spork, guard, guard-spork, capybara, capybara-webkit, database-cleaner, factory-girl, vcr, aruba, rcov, jasmine, and jasmine-rails. And there are lots of other tools that I could be using (cucumber, mocha, etc. etc.)

To me it feels like the world of Rails testing is out of control. This kind of complexity is why I left Java!!

Avatar

disclaimer: the cookie injection code is only an example.

Avatar

The :auth_token is "text plain" accessible in the cookie (since signing it would not help), capturing it would give access (a valid current_user) to any one who knows how to inject it later, no need to a password-based authorization process... wouldn't be wise to generate a new :auth_token for the current_user and save it before SessionsController#destroy?

(e.g.: you can inspect the cookie once and then later in any javascript console -> document.cookie("auth_token=7h3$u53r&H45H"); )

-> Kind of have no time to read all the comments, sorry if the subject have already been discussed.

Avatar

I started with the pre-release version of the new redcarpet gem and then went about modifying the helper you outlined in your screencast. Based on the following from the wiki for the gem

Rendering with the Markdown object is done through Markdown#render. Unlike in the RedCloth API, the text to render is passed as an argument and not stored inside the Markdown instance, to encourage reusability.

I tried to change your helper. This is the basics of what i tried. I clearly don't know enough to see what must be obvious to experienced programmers.

def markdown
  markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML,
        :autolink => true, :space_after_headers => true)
end

I have a policy model, so in my show view I tried something like.

markdown.render(@policies.introduction)

Needless to say, this isn't working correctly for me. Thoughts, help?

Avatar

I'd like to issue a warning:

This is nice for a very simple CRUD admin. Once you get to customizing it to fit your admin, it gets fairly involved fast.

The documentation comes up short, which shows in the amount of issues raised on github.

Ask yourself if you'd rather spend the time designing your own GUI rather than fussing with Activeadmin.

Avatar

Two questions: 1) how to display debug current_user. I went inside the gem and tried to insert it on the views but it doesn't display anything. 2) how to preset a value in the controller. I already tried in the regular controller and in the active admin controller override and it doesn't work: something like @website.user_id = current_user.id

Avatar

I have the same problem with spork. I'm working on a relatively new Rails 3.1 project and have just over 500 examples and running my specs with spork takes longer than without.

Avatar

Awesome! It pays to read the documentation. I'm looking through them now to see what else I missed...

Avatar

I could find the documentation for :focus in guard-spec ? where did you find it ?

Avatar

Ok, it seems like something that occurred in rails 3.1, that for some reason did not show its ugly face on normal rspec exec, but does on --drb.

mock_model should not be used with class name, rather it should be used with actual class now.

ruby
@order.station = mock_model("Station") #not good
@order.station = mock_model(Station) #all good 
Avatar

code is

ruby
@order = Order.new(:token => 'stam')
@order.station = mock_model("Station") #error occurs here 
Avatar

Great screencast. But - on spork, some of my specs fail, that do not fail on local (non drb) testing. Anyone has ideas?? :(

It seems it has something to do with rspec mocking, example of one of the errors (repeating itself over the entire spec file)

Failure/Error: @order.station = mock_model("Station")
NoMethodError:
undefined method `primary_key' for Station:Class

Avatar

There's also a little gotcha to even more decrease feedback time: just try add :bundler => false to both rspec-guard and spork-guard configurations in Guardfile and if you can run these commands directly (not through bundler), you'll see that total run time will be almost equal to testing time.
That's so because by default guard runs all commands through "bundle exec" which takes some time to initialize.

Avatar

Has anyone had any trouble getting spork to actually start running specs quicker?

My existing app's specs actually take longer to get running through spork and I can't figure out why.

I have even tried with a fresh rails 3.1 app with no actual specs, and running rspec via spork takes around 20 seconds vs 0.3 seconds without spork.

It's like there's some delay in rspec actually connecting to spork.

Avatar

I used the spork and guard for many month now, but just realized there is :focus and "return" to run all spec.

usualy i have to modify cucumber.yml to add tags. and run all by pressing ctrl +

thanks ryan

Avatar

Yeah.. Thank you Ryan.. This is the informative about testing.

I want to share this link for also spork for fast testing:
http://blog.carbonfive.com/2010/12/10/speedy-test-iterations-for-rails-3-with-spork-and-guard/

Avatar

Hi ryan,

Thanks for the screencast. How do I display the message from transaction associated with particular order? I would like to output the error in flash message.

For example:

if @order.purchase
....
else

flash[:error] = @order.transactions.response
format.html { render :action => "new" }
end

The code above doesn't work though.

Thanks!

Avatar

very interesting, very useful, very well explained... and rails code is very clear! - thank you

Avatar

I like the new queries much more than in 2.3, I find the chaining functionality the most useful. - thank you

Avatar

I'm in development mode. I can get faye working for my app however when I try from a different machine using the IP of my development machine. The changes do not occur. Any ideas why? I've tried to replace localhost with the IP address... but still no joy.

Avatar

there is always (at the very least) a small thing to learn or to remember to be aware of, even in the old well known episodes. - thanks

Avatar

Anybody able to get this working with an IPad?

Avatar

Exactly, @elomarns, but in this case we get ORDER BY 'id', not ORDER BY 'created_at' or another special column.

Avatar

When running tests, the block that sets the configuration for sorcery doesn't get run (I added puts "something" to make sure), so, while testing, sorcery uses username instead of email, because this line doesn't every run: user.username_attribute_name = :email.

Any ideas why or how to fix it?

Avatar

Yes, adding f.input :foo, :as => :string to form for such a field resolved the issue

Avatar

It seems that the error occurs while rendering input for field with custom pgsql type (something like CREATE TYPE foo_type AS ENUM ... )

Avatar

When I follow create or edit link I get the following error: "undefined method `_input' for #ActiveAdmin::FormBuilder:0x105b9a040" in gems/activeadmin-0.3.2/app/views/active_admin/resource/edit.html.arb

Using formtastic 1.2.4 seems to make no affect -- the error still occurs
Any ideas?

Avatar

Thank you .. Although a couple of years old, this is an excellent railscast !

Avatar

I get an error when I try view the admin page for my model. Any idea what could cause this?
undefined method `generate_association_input_name' for #ActiveAdmin::FilterFormBuilder:0x0000010411a440

Avatar

Thank you. This one is still very helpful four years later.

Avatar

this is almost exactly what i needed. Thanks, Ryan. One question though. I have a few models, and they can has_many :messages, but i'm creating these messages in an after_create callback, and not via a form, so i'm doing a Message.create in my callback, so question is in my messagable_id column do i simply put the id of the instance passed to the callback and messagable_type is just the class name?

Avatar

I'm trying to build a search form capable of full text searching as well as narrowing down based on booleans using Sunspot. However, i'm not able to get it working and the filter doesn't happen. Any input would be appreciated:

@search = Project.search do
   fulltext params[:search]
   facet(:master_bedroom)
   facet(:dining_room)
   facet(:bath)
   with(:master_bedroom, params[:mb]) if params[:mb].present?
   with(:dining_room, params[:dr]) if params[:dr].present?
   with(:bath, params[:p_bath]) if params[:p_bath].present?
 end    

i have the fields in the model:

searchable do
text :description
boolean :dining_room
boolean :bath
boolean :master_bedroom
end

and i have the following for my view:

<%= form_tag projects_path, :method => :get do %>

<%= text_field_tag :search, params[:search] %>
<%= check_box_tag :bath, 'true'%>
<%= submit_tag "Search", :name => nil %>

<% end %>

Avatar

Really nice screencast Ryan. Could you make a screencast for multiple file upload in Rails 3.1? It's so confusing for a lot of people ( myself included )!

Avatar

in your Gemfile just add gem 'table_builder' and from your console bundle install but I guess in a week you figured it out...