RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Just adding another +1 for EmberJS

Avatar

also just do a migration for hstore:

def self.up
execute "CREATE EXTENSION hstore"
end

def self.down
execute "DROP EXTENSION hstore"
end
end

Avatar

I learned more in those 9 minutes than in any other 9 minutes i can recall.

Avatar

Nevermind, Rails 4 beta as of today is back to using :child_index.

Avatar

Anyone getting this error on every single page? I can go to /auth/facebook and it redirects to Facebook for the auth, but when I come back I get this error. I also get this error on any other page I attempt to access:

"undefined method `helper_method' for main:Object"

Also, nothing populates in my users table.

This is coming from the bottom of application_controller.rb, which says in the show notes:

helper_method :current_user

Avatar

The error I get is "Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id"

Avatar

There should be noted, that this gem isn't aware of I18n. Because of this you should always write something like

<% cache [I18n.locale, @post] do %>
  <%= t(:post_created_at) %> <%= @post.created_at %>
<% end %>

in multilingual applications or write a helper for this. And there is no plans to support I18n in future. More info.

Avatar

It should be fairly simple. Most of the new stuff could be used in Rails 3.2 as gems.

I describe the process on this article: http://rubysource.com/get-your-app-ready-for-rails-4/

Avatar

This a newbie question:

I am getting an error if the user is not logged in. How do you enforce signup if they want to vote, while still allowing them to look at the page if they haven't signed up?

Avatar

Hi guys,

I am new to rails. How do I make this example show the data by hour?

My database has two relevant columns: "data_time" and "values". I have a few values per hour, so, I wanted to plot them all. I was able to group by hour changing:

group("date(data_time)")

to:

group("strftime('%Y-%m-%d %H', data_time)")

But the graph still shows only per day. I suspect it has something to do with:

(start.to_date..Date.today).map do |date|

but I can't figure out what it is!

Avatar

Better solution is to use ajax. It's easier to read now and programmer knows what's going on.
When you are using the ".erb" code, it out of context and there are two different places.

Avatar

I'm having problems to show the tickets after pop3 ativation.

Encoding::CompatibilityError in Tickets#index

Showing /home/caio/RubymineProjects/tickets/app/views/tickets/index.html.erb where line #17 raised:

incompatible character encodings: UTF-8 and ASCII-8BIT
Extracted source (around line #17):

14:
15: <%= ticket.Subject %>
16: <%= ticket.From %>
17: <%= ticket.Body %>
18: <%= link_to 'Show', ticket %>
19: <%= link_to 'Edit', edit_ticket_path(ticket) %>
20: <%= link_to 'Destroy', ticket, method: :delete, data: { confirm: 'Are you sure?' } %>
Rails.root: /home/caio/RubymineProjects/tickets

Application Trace | Framework Trace | Full Trace
app/views/tickets/index.html.erb:17:in block in _app_views_tickets_index_html_erb___2407065071369385486_24584740'
app/views/tickets/index.html.erb:13:in
each'
app/views/tickets/index.html.erb:13:in _app_views_tickets_index_html_erb___2407065071369385486_24584740'
app/controllers/tickets_controller.rb:7:in
index'

I'm googling a lot but I've found nothing...

Avatar

How can u get rid of the "page=1" on the url link?

Avatar

Hi,

I'm trying to test specific extensions to an engine I mounted in my app.

I wanted to have a look at the puppy project you mention, but the repo is not there anymore. Do you mind sharing that project or the rationale that made it work?

Thank you very much

Avatar

Tried this. Looks great, but requires a lot of effort to fit in my environment (I18n, etc.).

Avatar

For anybody that's getting bitten by the "redundant UTF-8 sequence" bug when attempting to upload an Excel file (which is likely causing the error Ryan refers to), a fix has been implemented in the 3-2-stable branch of rails, but not released yet. In order to get this goodness into your own app, simply create this file:

ruby
module ActiveSupport::JSON::Encoding
  def self.escape(string)
    if string.respond_to?(:force_encoding)
      string = string.encode(::Encoding::UTF_8, :undef => :replace).force_encoding(::Encoding::BINARY)
    end
    json = string.gsub(escape_regex) { |s| ESCAPED_CHARS[s] }
    json = %("#{json}")
    json.force_encoding(::Encoding::UTF_8) if json.respond_to?(:force_encoding)
    json
  end
end

...and be sure te require it. You should be good to go, after that.

Avatar

Does anyone know how integrate passenger with virtualhostx ?

Avatar

Shouldn't that be include Concerns::Userable and module Concerns::Userable? Rails autoloading assumes directories correlate to namespaces.

Avatar

Yes i am deploying to Heroku. I will check it out. Thanks for the info.

Avatar

I get the error "can not update on a new record object" for the method index_term on the increment!. This makes sense but I am not sure of the best way to fix it other than using first_or_create. Suggestions(hehe)?

Avatar

This is how to support the edit form. Notice the trigger method.

Description: Execute all handlers and behaviors attached to the matched elements for the given event type.

ruby
jQuery ->
  loaded_model = $('#merchant_neighborhood_id :selected').text()
  if loaded_model.length < 1
    $('#merchant_neighborhood_id').parent().hide()
  models = $('#merchant_neighborhood_id').html()
  $('#merchant_city_id').change ->
    make = $('#merchant_city_id :selected').text()
    escaped_make = make.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
    options = $(models).filter("optgroup[label='#{escaped_make}']").html()
    if options
      $('#merchant_neighborhood_id').html(options)
      $('#merchant_neighborhood_id').parent().show()
    else
      $('#merchant_neighborhood_id').empty()
      $('#merchant_neighborhood_id').parent().hide()

  $("#merchant_city_id").trigger "change"
Avatar

I'm facing the same issue, could you solve it?

Avatar

No, the updated_at timestamp is coming from the object you've already pulled... specifically this is generated using the cache_key method on your object. Then it's generating an MD5 of your template to see if it's changed and adding that into the key being stored in memcache.

Avatar

What about utf-8? I cannot see Turkish characters in pdf file :/

Avatar

you would want to use a scheduler of some sort that calls the rake task every fixed interval of time. check out the Whenever gem, or if you're deploying on Heroku there's an easy to use scheduler add-on.

Avatar

Use rake task. It's great for doing this kind of job.

Avatar

I can't get the javascript to load. I mean, the javascript file I see in my code, but then I can't get anything to call the best_in_place thing properly. Everything errors saying it isn't there.

Bashing head into wall. Debugging Javascript is impossible.

Avatar

I knew my problem now ...your idea is really cool and saved many days of search...
I used next_page with a ,,,but after adding 'a' its working now

Avatar

What if no associated records exist? How would I use this in a new form where neither table has any records, to create new records?

Avatar

cant you just do
alias_method :twitter, :all
alias_method :facebook, :all ?

Avatar

how do I display a controller validation error on the liquid page once a user submits a form? basically how do i rerender the liquid page with the exception as a variable in the template?

thx

Avatar

Amazing episode Ryan, as always !

And I love your last words about others skills than programming ! I personally want to make music in 2013 :)

If you are interested in listening to my music, check out https://soundcloud.com/thibaut-assus-1 :)

I would be also amazed that you could come Ryan to Paris.rb ! I have a couch and I can show you some amazing places in of Paris ! => http://meetup.rubyparis.org/

Happy new year, and stay how you are, amazing and so helpful !

Avatar

Great episode, building off this for auto detection...
** How do you convert js values received as TZInfo identifiers to Rails TimeZone name/key? **

FROM: "America/New_York" returned from JavaScript TZinfo detection
TO: "Eastern Time (US & Canada)" convention used in Rails TimeZone

http://stackoverflow.com/questions/14221615/how-to-convert-tzinfo-identifier-to-rails-timezone-name-key

Thanks!
-E

Avatar

Hi, Clemens.
I also met this problem. Did you find a solution?
Thanks.

Avatar

Be careful with simple_form

Took me awhile to discover that you can't login if your form is built with simple_form.
My sessions controller is in an admin namespace and the builder gave me "/admin/sessions[password]".

Avatar

I quickly run in to some kind of brick wall when trying this out, TDD style ofc ;) This blog post, by someone running faster than me, cleared most of it up: http://www.superpumpup.com/blog/1079982-postgresql-schema-multi-tenancy-and-rspec-testing/

So if you are having troubles with Database Cleaner not being cleanly enough with your schemas, give it a read.

Avatar

your method is really nice ,,,I tried it ,,but with internationalization ,,,its not working ,,,on console it gives me the get method for url is ok ..but no appending happens for pagination ...I am using kaminari so , and I used

ruby
$("#posts").append("<%= escape_javascript(render(@posts)) %>"
<% if (@posts.current_page < @posts.num_pages) %>
  $('.pagination').replaceWith('<%= j paginate(@posts) %>');
<% else %>
  $('.pagination').remove();
<% end %>
Avatar

your method is really nice ,,,I tried it ,,but with internationalization ,,,its not working ,,,on console it gives me the get method for url is ok ..but no appending happens for pagination ...I am using kaminari so I replaced .next_page with .next a , and I used

ruby
$("#posts").append("<%= escape_javascript(render(@posts)) %>"
<% if (@posts.current_page < @posts.num_pages) %>
  $('.pagination').replaceWith('<%= j paginate(@posts) %>');
<% else %>
  $('.pagination').remove();
<% end %>
Avatar

I've messed with both.
It should be a fairly simple transition, assuming you aren't using things that have been deprecated in Rails 3 (which would be removed in Rails 4)... and of course assuming you are at least on Rails 3.1. The Asset Pipeline is a fairly large hurdle.

Avatar

You must 'CREATE EXTENSION hstore' first

Avatar

maybe the search path is not being set correctly??

Avatar

I'm running into this error, but I want to make this work without using apartment for now. any ideas?

Avatar

Same here!
Both in Rails 3.2.8 and 3.2.10

Gemfile:

source 'https://rubygems.org'

gem 'rails'
gem 'mysql2'

group :test, :development do
gem "rspec-rails", "~> 2.6"
gem 'factory_girl_rails'
end

group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'

gem 'uglifier', '>= 1.0.3'
gem 'jquery-datatables-rails'
gem 'jquery-ui-rails'
end

gem 'jquery-rails'
gem 'prawn'
gem 'kaminari'

Avatar

Same problem here. Has anyone found a workaround?

Avatar

Hi Ryan, thanks for another awesome tutorial! Great! :) Just wanted to add… if anyone's having trouble getting terminal colors working after creating ~/.bash_profile, you need to run source ~/.bashrc each time you login. Or you can add the line to ~/.bash_profile by typing the following in terminal:

echo 'source ~/.bashrc' >> ~/.bash_profile
Avatar

Ok adding group_ids as attr_accessible in my User Model solve the problem !

Sorry :p

Avatar

He talks about this in the video: " If we used a before_filter here instead the time zone would leak through to other requests so it’s necessary to use an around filter instead."

Avatar

Thanks for this great screencast.

Got an issue with my app.

Checkboxes well diplayed, but when I update, got a message "Can't mass-assign protected attributes: group_ids"

So, I try to manage membership.

Got users and groups.

membership model :

ruby
class Membership < ActiveRecord::Base
  attr_accessible :created_at, :group_id, :position, :user_id
  
  belongs_to :group
  belongs_to :user
  
end

User model :

ruby
  attr_accessible :client_id, :email, :firstname, :group_id, :lastname, :password, :password_confirmation, :role_id
  has_many :memberships
  has_many :groups, :through => :memberships

Group model

ruby
  attr_accessible :name, :client_id, :description, :user_id
  
  has_many :memberships
  has_many :users, :through => :memberships

Here the code in my form

ruby
<%= hidden_field_tag "user[group_ids][]", nil %>
  <% Group.all.each do |group| %>
          <%= check_box_tag "user[group_ids][]", group.id, @user.group_ids.include?(group.id), id: dom_id(group) %>
          <%= label_tag dom_id(group), group.name %><br>
  <% end %>

Did I miss something ?

Thanks