RailsCasts Pro episodes are now free!

Learn more or hide this

Dennis Ideler's Profile

GitHub User: dideler

Site: www.dennisideler.com

Comments by Dennis Ideler

Avatar

Raygun is yet another option.

Does the exception_notification gem send notifications for rescued exceptions?

Edit: You have to explicitly send notifications for rescued exceptions.

Avatar

If you don't want Factory Girl to hit your database, you can try using build_stubbed.

ruby
user = FactoryGirl.build_stubbed(:user)
Avatar

Why is the guard-rspec gem in the test environment group, while on the project's README it says to put it in the development group?

Avatar

In addition to the reasons that gaeldeest mentioned:

  • manual testing is slow if you have to repeat the manual test every time you make a change that could affect it
  • manual testing is error prone
    • you might make a typo and not notice the mistake
    • maybe you test it differently every time, instead of being consistent
    • others cannot review the tests and see that testing is being done correctly
Avatar

Charles, thanks for writing that. It perfectly describes how I feel and what I've observed as a new Rails developer.

Avatar

one assertion per test makes sense

Can you explain why? It's not obvious to me.

Avatar

So far we've been starting Delayed Job by running rake jobs:work but in production we should use the delayed_job script provided in the script folder.

The reason why is explained here https://github.com/collectiveidea/delayed_job/issues/659.

Avatar

You should be able to just include gem 'haml-rails' in your Gemfile now and have it work.

http://stackoverflow.com/questions/18485147/haml-rails-on-rails-4-0

Avatar

Looks like it. The original projects seem to be unmaintained; the last commits were made years ago.

Avatar

Ignore my above comment, you can simply use @items.offset + 1 as your starting count :)

Avatar

One more thing!

If you're displaying the total number of records found like @items.size, then paginate will now show an incorrect count. That will show the count for records on current page.

To get the count of all records found by the query, you'll want to do @items.total_entries.

Avatar

Also, if you are displaying record counts in your views, those counts will now reset on every page. Here's how to get the starting count of the current page, from which you can easily continue the count for the current records displayed.

In this example, the count is a cell in a table row.

Before

ruby
#app/views/items/index.html.haml
...
%tbody
  - @items.each_with_index do |item, i|
    %tr
      %td= i + 1
...

After

ruby
#app/helpers/application_helper.rb
def record_count_start(records)
  ((records.current_page - 1) * 30) + 1
end

#app/views/items/index.html.haml
...
%tbody
  - i = record_count_start(@items)
  - @items.each do |item|
    %tr
      %td= i
    - i += 1
...

Note my use of current_page. Other useful stuff that will_paginate provides:

  • total_pages
  • per_page
  • offset
  • previous_page
  • next_page

To get the number of records on the current page, you can use size.

Avatar

If you are displaying search results, you can paginate like this.

Before

ruby
# app/controllers/items_controller.rb
def index
  @items = Item.search(params)
end

After

ruby
# app/controllers/items_controller.rb
def index
  @items = Item.paginate(page: params[:page]).search(params)
end
Avatar

Thanks! I am using Postgres and you were right, converting the arrays from message.to and message.from to strings or getting the first elements solves the issue.

Avatar

I can't get past the step where you create a new record using the email contents.

ruby
Mailman::Application.run do
  default do
    puts message.from
    puts message.to
    puts message.subject
    puts message.body # or body.decoded, both work
    # The above works, but the below fails.
    Email.create(to: message.to,
                 from: message.from,
                 subject: message.subject,
                 body: message.body.decoded)
  end
end

I get the error type_cast': can't cast Mail::AddressContainer to string.

Full stack trace here.

Avatar

I'm confused about a few things.

  • Will this work with Rails 4?

  • You specify require: false for the mailman gem because you don't want it loaded in Rails but as a separate process. Is this how mailman is meant to be used? No where on mailman's userguide does it mention this.

  • There is no script directory in Rails 4? Sure I can just create it, but I imagine there is a better place to put stuff like the mailman_server file?

Avatar

I just want to point out that you should use true and false for any boolean fields that you have. As seen in the ASCIIcast:

form.select :discontinued, [["Yes", true], ["No", false]]

You may be tempted to use 1 and 0 since those are the values used in the database for a TINYINT, but Rails will not show the correct default option for the select menu if you use integers. So make sure you use booleans!

Avatar

Also, you can use search_field_tag instead of text_field_tag. That will create a text field of type "search".

Avatar

Also, that last part about adding more fields to the search came in very handy, thanks! I was able to add a dropdown list to filter results in addition to the search.

Avatar

Is this prone to SQL injection? It looks like you're directly embedding strings into your SQL queries.

Avatar

For anyone who's using Rails 4 and is wondering about projects_path, don't worry. I'm a Rails newbie, but in my experience, Rails adds what you need to your routes file (app/models/modelname.rb) automatically when you create the controller.

If it doesn't, I believe adding the following line does it:

ruby
resources :projects   # Replace "projects" with your controller name

Also, if you're wondering why we use def self.search instead of def search, it's because we want a class method instead of an instance method. In other words, it allows us to do Project.search, we're calling the method from the class, not from an instance of the class.

Avatar

That probably means you're missing the search method in your model.

Avatar

I noticed this screencast is pretty old. As some have mentioned, there's a faster faker (ffaker) now.

When I search through repos on GitHub, I noticed a lot of people are using the ffaker gem in their specs/ directory.

I'm new to Rails, and I'm not familiar with the specs/ directory yet. Is the info in this screencast still a good way to populate your DB with random fake data?