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.
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.rbdefrecord_count_start(records)
((records.current_page - 1) * 30) + 1end#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.
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.
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)
endend
I get the error type_cast': can't cast Mail::AddressContainer to string.
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?
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!
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.
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.
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?
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.
If you don't want Factory Girl to hit your database, you can try using
build_stubbed
.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?In addition to the reasons that gaeldeest mentioned:
Charles, thanks for writing that. It perfectly describes how I feel and what I've observed as a new Rails developer.
Can you explain why? It's not obvious to me.
The reason why is explained here https://github.com/collectiveidea/delayed_job/issues/659.
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
Looks like it. The original projects seem to be unmaintained; the last commits were made years ago.
Ignore my above comment, you can simply use
@items.offset + 1
as your starting count :)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
.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
After
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
.If you are displaying search results, you can paginate like this.
Before
After
Thanks! I am using Postgres and you were right, converting the arrays from
message.to
andmessage.from
to strings or getting the first elements solves the issue.I can't get past the step where you create a new record using the email contents.
I get the error
type_cast': can't cast Mail::AddressContainer to string
.Full stack trace here.
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?I just want to point out that you should use
true
andfalse
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!
Also, you can use
search_field_tag
instead oftext_field_tag
. That will create a text field of type "search".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.
Is this prone to SQL injection? It looks like you're directly embedding strings into your SQL queries.
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:
Also, if you're wondering why we use
def self.search
instead ofdef search
, it's because we want a class method instead of an instance method. In other words, it allows us to doProject.search
, we're calling the method from the class, not from an instance of the class.That probably means you're missing the search method in your model.
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?