RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

I have a crash in mac that I resolve doing this in my Gemfile:
gem 'rmagick', :require => 'RMagick'

Avatar

Thank you for posting that! I was getting the error
uninitialized constant Sprockets::LazyCompressor
and couldn't find the answer anywhere else

Avatar

Thank you, I tried this I have no idea why it's not working still. I'm going to put it aside and try it later, its like the app isn't even trying to send an email, yet it works in development.

Avatar

Did you find a solution for this? Looking for the same.

Avatar

Excellent Railscast. But for those who don't want to keep querying server, you can use DataTables and do sorting and searching on the client side. Example here: http://jsfiddle.net/GJYfe/1/

Avatar

Just to add up more info for future visitors:

You can, instead of "include" use "preload". But this will cause a second query to be triggered. This method is recommended since it'll help you to avoid N+1 queries.

Avatar

Unfortunately eager loading polymorphic associations isn't actually possible. The polymorphic association holds a a class name and an id present on that class. The polymorphic associations, though, don't have a way to join them in a SQL query since SQL doesn't support any sort of meta-programing.

For instance:

Address is kept in addresses table, and has an addressable reference. In the database there would be "addressable_id" and "addressable_type". There's no way to use "addressable_type" to make an INNER JOIN in the SQL query.

Avatar

hello guys i am niewbie so

i am get some confused during installation of refinerycms.some how i've installed refinerycms but when excute the code there is no authentication process and no files in my app directory it is running gems installed path.but companyname, home, about pages are displayed.

i am using Windows 8 os. how can i get the code run in my app dir.

please kindly help me to learn about refinery cms dudes.

Avatar

Just found that you cannot use the - character either in the environment name.

I tried to use staging-02 but had to settle for staging2.

Avatar

Did you ever resolve this? Thanks for any tips.

Avatar

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

Avatar

I followed this video to import CSV file, but I cannot insert data from file to table:
SQL (0.6ms) INSERT INTO products (created_at, id, updated_at) VALUES ('2014-04-07 03:16:07', 27, '2014-04-07 03:16:07')
other fields were not insert in this command. I'm using rails 4 & ruby 2.0

Avatar

Not sure when Rails added this, but there's a much simpler way now:

ruby
<%= f.collection_check_boxes :category_ids, Category.all, :id, :name %>
Avatar

Did you ever figure out a solution to this? I'd also like to be able to save a user_id with the token record getting created.

Avatar

I am not one for commenting on videos, but you tutorials are short and to the point, its makeing my unversity project much more advanced with very little pain.

You one of best for showing off rails, but i hate it when i try some code out and it doesnot work cause rails 4 has change somthing, i guess that what learning programmer is ment for. :)

Thanks

Avatar

SyntaxError: unexpected indentation
(in C:/Users/MikeMarskal/SourceCode/RailsProjects/v193/Learning/Railscasts/FromGithub/323-backbone-on-rails-part-1/raffler-after/app/assets/templates/entries/index.jst.eco)

This Error happens when I follow along AND also when i download this railscast from github. New to coffee and barebones. Any ideas?

Avatar

Thanks, i was looking how to fix that

Avatar

JohnsCurry:

in my case- (i may not be completely pristine with this railscast)

config/initializers/setup_mail.rb
ActionMailer::Base.default_url_options[:host] = "www.productionsite.com"

and

config/environments/production.rb
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.default_url_options = { :host => "www.productionsite.com" }
Avatar

I like the screencast... It works, but how do you get it to work in production? like specifically? I've tried to figure out what to put in my production.rb file but can't find a single thing telling me what to do.

Avatar

Group the activities by User and Action.

Avatar

Finally i add a counter for clean the session, after a couple of refreshes or a couple of errors it will show the normal page

user.rb
  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.email = auth.info.email
    end
  end

  #persist user in session on validation error with omniauth
  def self.new_with_session(params, session)
    if session["devise.user_attributes"]
      new(session["devise.user_attributes"], without_protection: true) do |user|
        user.attributes = params
        user.valid?
        session["count_errors"] = session["count_errors"] + 1
        session["devise.user_attributes"] = nil if session["count_errors"] == 2
      end
    else
      super
    end
  end

  #validation for password on omniauth
  def password_required?
    super && self.provider.blank?
  end

  #password for update if blank on omniauth
  def update_with_password(params, *options)
    if encrypted_password.blank?
      update_attributes(params, *options)
    else
      super
    end
  end

  def has_no_password?
    self.encrypted_password.blank?
  end
omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def all
      # You need to implement the method below in your model (e.g. app/models/user.rb)
      user = User.from_omniauth(request.env["omniauth.auth"])

      if user.persisted?
        flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => user.provider.titleize.split(" ").first
        sign_in_and_redirect user, :event => :authentication
      else
        session["count_errors"] = 0 if session["devise.user_attributes"] == nil
        session["devise.user_attributes"] = user.attributes
        redirect_to new_user_registration_url
      end
  end
  alias_method :twitter, :all
  alias_method :google_oauth2, :all
end
Avatar

Hi, i'm having troubles with password_required and new_with_session

user.rb
  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.email = auth.info.email
    end
  end

  #persist user in session on validation error with omniauth
  def self.new_with_session(params, session)
    if session["devise.user_attributes"]
      new(session["devise.user_attributes"], without_protection: true) do |user|
        user.attributes = params
        user.valid?
      end
    else
      super
    end
  end

  #validation for password on omniauth
  def password_required?
    super && self.provider.blank?
  end

  #password for update if blank on omniauth
  def update_with_password(params, *options)
    if encrypted_password.blank?
      update_attributes(params, *options)
    else
      super
    end
  end

  def has_no_password?
    self.encrypted_password.blank?
  end

The session are always there, imagine this case, you sign up with twitter, the system asks you for a email, you regrets and want to use normal register, you can't because there isn't the password field and the provider info is still in cache, i need to know where to clear that session :/

Similar problem when you want to update the blank password with an error, it would f.object.encrypted_password.present? would return true in that case, so i add that has_no_password? method for that.

Avatar

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

Avatar

Same here. Hit a dead end because of this.

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

Does anyone have an example they could post that lists all of the projects, like in index.html.erb, instead of just 1 project (like the tutorial using #show).

Thank You.

Avatar

Ok, I've done it:

_calendar.html.erb:

<%= link_to "<", {date: @date.prev_month}, id: "calendar-main-month-left-arrow", remote: true %>
<%= @date.strftime("%B %Y") %>
<%= link_to ">", {date: @date.next_month}, id: "calendar-main-month-right-arrow", remote: true %>

<%= render "calendar_days" %>

_calendar_days.html.erb:
(Here you place just the rendering of the helper starting with
<%= calendar @date do |date| %>
and finishing with
<% end %>

date.js.coffee:
$("#calendar-main-month-left-arrow").attr("href", "/?date=<%= @prev_date %>&prev=true")
$("#calendar-main-month-right-arrow").attr("href", "/?date=<%= @next_date %>&next=true")
$("#calendar-days").replaceWith("<%= j render 'calendar_days' %>")
$("#calendar-main-month-span").html("<%= j @date.strftime('%B %Y') %>")

and in your controller:
if params[:date]
@prev_date = Date.strptime(params[:date],"%Y-%m-%d") - 1.month
@date = Date.strptime(params[:date],"%Y-%m-%d")
@next_date = Date.strptime(params[:date],"%Y-%m-%d") + 1.month
render "date"
end

Let me know if it helps ;), you can help yourself with episode #136 (revised)

Avatar

Proper indentation is often the solution to a coffeescript problem

Avatar

what about content_for :head included scripts ?

Avatar

After update rails version to 3.2.17, the javascript and css can work well.

Avatar

Hello,

I tried to import an excel file but I got this error:Couldn't find User without an ID. I am using rails 4 & ruby 2.1.1.

Avatar

For me this worked:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

That dot is necessary.

Avatar

You may want to ask this on Stackoverflow. Then you can provide some more details about your setup.

(If you haven't figured it out already :)

Avatar

i had some problems but it was because i was trying to render a file/object not an attribute, something like:

json.id @image.id
json.name @image.name
json.address @image.image

(using carrierwave)

then even tho it was ok and not rising any error it didnt know how to render a file object, I had to do:

json.address @image.image.route

Avatar

Hi John,
I'm in the same boat. Did you ever get this working?

Avatar

I think the reason it didn't work is because you changed :cmd => "_cart" to :cmd => "_xclick"

Just put it back the way Ryan had it for the model, only the view that has the "encrypted" hidden field will need a "cmd" hidden field paired with it ... the value of which should be "_s-xclick"

So the problem wasn't merge, the problem was the wrong command in the encrypted payload and paypal didn't know what you wanted to have done.

rich

Avatar

No, when you register for sandbox you will get an email/id generated just for the sandbox. You will need to log in with your regular paypal account and goto the developer section and manage your accounts. Change password for the manager / business account and then login to http://www.sandbox.paypal.com using that special email and password.

It will look just like production without being production. There are different certificate stores for sandbox and production.

rich

Avatar

I think I have found a solution for (nested attributes) plain HTML5 multiple file uploads, with no tokens, hashes...etc

Please leave feedback

Multiple Files Upload With Nested Resource Using Paperclip in Rails

Avatar

Hey guys, is there anyone who can help me installing the mercury editor on _form.html.erb? I want to use it when writing articles for my blog.