Receiving exceptions by email is great because you can set it up to be notified when this happens. Also if you're providing an in-app interface for browsing exceptions you need to worry about locking it down. Having a database of exceptions is handy, but Gmail search works pretty well.
Even just a few tests are better than no tests at all, so start off with the easiest ones to write that will get you the most "bang for your buck".
I find these are generally the high-level request specs like I show in episode 275. Writing request specs is nearly as easy as walking through the app manually, and once it's done you won't have to manually test it next time.
This is a controversial subject, but I don't subscribe to the "one assertion per test" way of thinking. It can really slow down tests if you're not sharing data between assertions, especially in high-level request specs. RSpec 2 shows you exactly what line failed the assertion so it is easy to spot in a multi-assert clause.
Right, without PJAX I would normally add remote: true option to a link and respond with some JavaScript that renders out only exactly what I need, but that is quite a bit more work. So it is a tradeoff of performance for convenience.
Since this screencast I use the request specs for my development.
It works great for understanding of the function and as a direct mapping with use cases from RE.
The only issues I have is speed.
As for most of the specs I need a logged in user. This is therefore created in nearly every before block. Also, the user factory creates certain associated models as well (in this case a company.)
Is there a better/faster way to do it?
Would you use fixtures for those cases?
ruby
describe "Request Management Requests"do
before do@user = create(:user)
login @userend
it "lists open requests"do@req = create(:request, :company => @user.company)
visit requests_path
page.should have_content(@req.name)
endend
Looks good, but: this always renders the full layout? That does seem very wasteful to me. But I guess its a tradeoff between code complexity and performance.
Just as I suspected. When I downloaded the jQuery file to my public/javascripts/ folder, Jasmine worked just fine. You can use this as a temporary hack.
I don't think there's support for Jasmine and Rails 3.1 yet if you are using the asset pipeline. I'm trying to figure out why my tests aren't working, and it seems to me it is because the jQuery files are loaded when the pages of my Rails app load.
You've inspired me to give Unicorn a try. Quick question: Don't you need to run the following after you've created the /etc/init.d/unicorn symlink to unicorn_init.sh so that Unicorn automatically starts when the server is rebooted?
bash
sudo /usr/sbin/update-rc.d -f unicorn defaults
I found this to be the case for a production server I'm running on Ubuntu. Can't wait to see more episodes on deployment. Thanks!
I know that sounds like a cop out but I had to force myself to just continue to test. Don't feel like you have to hit that holy grail of 100% coverage or even 50%, just keep testing as much as you know how to. The more you do it, the more you'll eventually get to the point that you realize you can code as fast, if not faster through tests and when you realize that, its an exciting thing!
Late to the party -- I like Ryan's approach but was troubled by the complexity of the create() method. My modified approach has a Wizard model that saves the current_step as a db field (so it's preserved across sessions) and saves an instance before starting. From there, everything is done in the upate() method, so the controller simplifies down to:
ruby
defshow
render @wizard.current_step
enddefupdate
(params[:step] == 'back') ? @wizard.step_back : @wizard.step_forward
if@wizard.update_attributes(params[:wizard])
redirect_to wizard_path, :notice => 'update succeeded'else# the reload restores the previous (valid) step as the current step
render @wizard.reload.current_step
endend
The only other substantive change is the next / prev buttons in the views, which might look like this:
I am trying to make a chat on my website looking like google talk. I
want to manage the disconnect event. A user can disconnect on several
way :
By clicking the disconnect button (easy to push the event to his
friends)
By close his windows (I can send the event to windows close
javascript event but if the user has many windows open on my website,
i don't want to disconnect him.)
So, how can i detect when my connection on a channel is closed? Each
user has his own channel build with his id.
Ok found the issue. Apparently the gem ActiveAdmin (covered in a previous episode) causes conflict because it uses meta_search gem in the gemfile.lock, which makes Product.search use meta_search. A workaround to this is
it seems like the @search always has a full collection of all the products no matter what string I pass it.
Here is how I declared searchable in my product.rb file
searchable do
text :title
end
and here is what my index action looks like in the products_controller.rb
def index
@search = Product.search do
fulltext params[:search]
end
@products = @search.results
respond_to do |format|
format.html
format.xml { render :xml => @products }
end
end
After watching the video, I'm slightly confused. It seems that presenters more or less add another layer of complexity to the view concept. Is there some convention that if your controller and view are below a certain complexity level that a presenter isn't a best choice? When would I want to use a presenter vs leaving the show action with a little more data in it?
Also, are presenters main-stream or becoming main-stream? I've heard little about them until recently. Are they something that will most likely stick and become a standard convention?
Looks like something worth learning more about, but not sure I'm fully convinced they are very useful, but that's probably just my lack of experience with them speaking.
So I see lots of people asking about roles and limiting what a specific user can see. Has anyone found a way to do this with active admin? At this point I'm not sure it's easily done.
Thanks for a great tutorial Ryan. I've noticed this method, when using Heroku, means that login details are case sensitive. I think it's because of the way Postgres works - has anyone found a way to adapt the code to allow login details to not be case-sensitive for Heroku?
Got a site I'm working on and have followed the example above and got the authentication working except for the logout link, with the sessions#destroy not deleting the session and the redirect is going to the login page not the root_url.
+1 Really looking forward to the Capistrano version. I've been running Unicorn + nginx for a little while now and I still learned a bunch from this intro railscast. Thanks again!
I think ruby users on win32 (RubyInstaller & DevKit) will have to do "gem install ffi -v=1.0.9" before "gem install vagrant". There are issues with the current version of this virtualbox - dependency: https://github.com/ffi/ffi/issues/167
Receiving exceptions by email is great because you can set it up to be notified when this happens. Also if you're providing an in-app interface for browsing exceptions you need to worry about locking it down. Having a database of exceptions is handy, but Gmail search works pretty well.
Even just a few tests are better than no tests at all, so start off with the easiest ones to write that will get you the most "bang for your buck".
I find these are generally the high-level request specs like I show in episode 275. Writing request specs is nearly as easy as walking through the app manually, and once it's done you won't have to manually test it next time.
This is a controversial subject, but I don't subscribe to the "one assertion per test" way of thinking. It can really slow down tests if you're not sharing data between assertions, especially in high-level request specs. RSpec 2 shows you exactly what line failed the assertion so it is easy to spot in a multi-assert clause.
Right, without PJAX I would normally add
remote: true
option to a link and respond with some JavaScript that renders out only exactly what I need, but that is quite a bit more work. So it is a tradeoff of performance for convenience.Totally blown away by this. It makes AJAX so much easier, the way it should be, thank you
Since this screencast I use the request specs for my development.
It works great for understanding of the function and as a direct mapping with use cases from RE.
The only issues I have is speed.
As for most of the specs I need a logged in user. This is therefore created in nearly every before block. Also, the user factory creates certain associated models as well (in this case a company.)
Is there a better/faster way to do it?
Would you use fixtures for those cases?
So after perusing the github pages for pjax, it seems like you could do this:
pjax is cool and all, but what about adding transition effects?
Really interesting. I sometimes used Poirot for this purpose(https://github.com/olivernn/poirot).
Just a notice: isn't "try" a perf killer? You use it in your product helper. (Well just did some benchmark, seems not to be that a problem)
Looks good, but: this always renders the full layout? That does seem very wasteful to me. But I guess its a tradeoff between code complexity and performance.
Just as I suspected. When I downloaded the jQuery file to my public/javascripts/ folder, Jasmine worked just fine. You can use this as a temporary hack.
I don't think there's support for Jasmine and Rails 3.1 yet if you are using the asset pipeline. I'm trying to figure out why my tests aren't working, and it seems to me it is because the jQuery files are loaded when the pages of my Rails app load.
What is the way to use Sunspot/Solr with mutiple fields ?
It works fine with a simple form, as explained in the screencast.
For example, I'm looking for how to make a search engine :
<%= form_tag products_search_path, :method => :get do %>
<%= label_tag "Location ?" %>
<%= select_tag :address, "new york".html_safe %>
<%= label_tag "Type ?" %>
<%= text_field_tag :model, params[:type] %>
<%#= select_tag :model, "testtest".html_safe %>
<%= label_tag "Category ?" %>
<%= text_field_tag :category, params[:category] %>
<%= submit_tag "Search" %>
<% end %>
Thanks guys!
i was waiting for such a post and its information!
Owner of london escorts
is it also possible to run multiple ruby versions at the same time? (e.g. 1.8.7 and 1.9.2)
You've inspired me to give Unicorn a try. Quick question: Don't you need to run the following after you've created the /etc/init.d/unicorn symlink to unicorn_init.sh so that Unicorn automatically starts when the server is rebooted?
I found this to be the case for a production server I'm running on Ubuntu. Can't wait to see more episodes on deployment. Thanks!
Keep testing!
I know that sounds like a cop out but I had to force myself to just continue to test. Don't feel like you have to hit that holy grail of 100% coverage or even 50%, just keep testing as much as you know how to. The more you do it, the more you'll eventually get to the point that you realize you can code as fast, if not faster through tests and when you realize that, its an exciting thing!
This doesn't seem to work with FactoryGirl. When I turn transactional fixtures off, all of my specs depending on FactoryGirl fail. :'(
Thanks for this episode, Ryan!
I have only one question: using two (or more)
should
in oneit
is o'key or it's just an example?Hi Gregor.
This is really interesting. Any chance that you can share a config example for setting up nginx_http_push_module with faye?
Luke, build an app then come back after a year and try to make some changes. You'll wish you had written some tests.
I still feel like testing is such a drag, How do I believe?
Short answer (via jquery.com) is: PUT and DELETE are not supported by all browsers.
It's more convenient to go with POST.
What other considerations should be taken into account to use the PUT method, instead of POST? Is it as simple as changing the method name?
How can I change the password?
I need to validate de "old_password" before change it.
Late to the party -- I like Ryan's approach but was troubled by the complexity of the create() method. My modified approach has a Wizard model that saves the current_step as a db field (so it's preserved across sessions) and saves an instance before starting. From there, everything is done in the upate() method, so the controller simplifies down to:
The only other substantive change is the next / prev buttons in the views, which might look like this:
Thanks for answer!
For running active_admin on Heroku add that line to your config/environments/production.rb.
And after that run
rake assets:precompile
, add public/assets to git, push to heroku and enjoy.Also check that option in production.rb (should be false for heroku).
config.assets.compile = false
Yes, i have the same problem but no solution.
Hello,
I am trying to make a chat on my website looking like google talk. I
want to manage the disconnect event. A user can disconnect on several
way :
So, how can i detect when my connection on a channel is closed? Each
user has his own channel build with his id.
Thank you for help.
who can help me?
class Attachment < ActiveRecord::Base
has_attached_file :att,
:url => "/files/:class/:id/:style/:basename.:extension",
:path => ":rails_root/public/files/systems/:id/:style/:basename.:extension"
end
class UserLogo < Attachment
has_one :user
has_attached_file :att,
:styles => { :original => "100x100>" } ,
:url => "/files/:class/:id/:style/:basename.:extension",
:path => ":rails_root/public/files/user_logos/:id/:style/:basename.:extension"
validates_attachment_content_type :att,
:content_type => ['image/png','image/jpg','image/bmp','image/jpeg','x-png','pjpeg']
end
Ok found the issue. Apparently the gem ActiveAdmin (covered in a previous episode) causes conflict because it uses meta_search gem in the gemfile.lock, which makes Product.search use meta_search. A workaround to this is
Sunspot.search(Product) do
I am using Rails 3.1 by the way if that matters
First, thanks for the awesome tutorial!
I am running in to this error when I try to access the @search.results in my controller: undefined method `results' for #MetaSearch::Searches::Product:0x129d527d8
it seems like the @search always has a full collection of all the products no matter what string I pass it.
Here is how I declared searchable in my product.rb file
searchable do
text :title
end
and here is what my index action looks like in the products_controller.rb
def index
@search = Product.search do
fulltext params[:search]
end
@products = @search.results
respond_to do |format|
format.html
format.xml { render :xml => @products }
end
end
any idea why I keep getting that error?
I'm wondering the same thing...saw you've asked this in a couple places on the web. Did you find an answer yet?
After watching the video, I'm slightly confused. It seems that presenters more or less add another layer of complexity to the view concept. Is there some convention that if your controller and view are below a certain complexity level that a presenter isn't a best choice? When would I want to use a presenter vs leaving the show action with a little more data in it?
Also, are presenters main-stream or becoming main-stream? I've heard little about them until recently. Are they something that will most likely stick and become a standard convention?
Looks like something worth learning more about, but not sure I'm fully convinced they are very useful, but that's probably just my lack of experience with them speaking.
I found it much easier to use rvm
Roles:
https://github.com/gregbell/active_admin/wiki/How-to-work-with-cancan
So I see lots of people asking about roles and limiting what a specific user can see. Has anyone found a way to do this with active admin? At this point I'm not sure it's easily done.
Great timing, I was just investigating the topic as I start witching to ruby 1.9 and wanted to keep that as painless as possible.
Can't wait for the deployment screencast now!
Thanks for a great tutorial Ryan. I've noticed this method, when using Heroku, means that login details are case sensitive. I think it's because of the way Postgres works - has anyone found a way to adapt the code to allow login details to not be case-sensitive for Heroku?
Looked over my code again and spotted an issue with my named route..... what a newbie!!!!!
Got a site I'm working on and have followed the example above and got the authentication working except for the logout link, with the sessions#destroy not deleting the session and the redirect is going to the login page not the root_url.
Any help with this would be appreciated.
+1 Really looking forward to the Capistrano version. I've been running Unicorn + nginx for a little while now and I still learned a bunch from this intro railscast. Thanks again!
hm, that wasn't Ryan Bates, right?. Screencast outsourcing? :-)
+1 For more deployment screencasts please! I'd be fascinated to see your deployment process.
What if you wanted to order by using ajax?
I know you posted this question 1 year ago, but it could be useful for some people.
RAND() is specific to MySQL, so if you are using PostgreSQL for instance it's random().
But are there any clear instructions on how to do so George? Please see my question on SO: http://stackoverflow.com/questions/7867574/rails-3-1-1-http-streaming-with-apache-passenger
I'd appreciate it if you could point me to some good reference.
I think ruby users on win32 (RubyInstaller & DevKit) will have to do "gem install ffi -v=1.0.9" before "gem install vagrant". There are issues with the current version of this virtualbox - dependency: https://github.com/ffi/ffi/issues/167
On the other hand the setup of vagrant on win64 runs well through jruby: http://vagrantup.com/docs/getting-started/setup/windows_x64.html