RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

The cache keys need to be unique in order to fetch its value later on.

So category_id is different for each category.

Think of the array [:category, category_id, :name] as syntactic sugar for what ends up being a cache key string.
For example:

ruby
"category/3/name" # the key that caches the category's name for the category_id 3
"category/4/name" # the key that caches the category's name for the category_id 4
"category/5/name" # the key that caches the category's name for the category_id 5

and so on...

Avatar

follow the instructions for the gem https://github.com/seyhunak/twitter-bootstrap-rails#installing-the-less-stylesheets

the first option, including 'therubyracer', 'less-rails' and 'twitter-bootstrap-rails' worked for me. The second didn't.

Avatar

I want to know how to post image

Avatar

I'm having difficulty finding out how to go about ordering the sub-tree navigation alphabetically?

Avatar

This is because Rails 4 uses Strong Parameters checking in the controller instead of the attr_accessible method in the model.

In order to use the code in this cast for Rails 4, you'll need to replace the code snippet here:

ruby
def self.import(file)
  spreadsheet = open_spreadsheet(file)
  header = spreadsheet.row(1)
  (2..spreadsheet.last_row).each do |i|
    row = Hash[[header, spreadsheet.row(i)].transpose]
    product = find_by_id(row["id"]) || new
    product.attributes = row.to_hash.slice(*accessible_attributes)
    product.save!
  end
end

With something that looks like this:

ruby
def self.import(file)
  allowed_attributes = [ "id","name","released_on","price","created_at","updated_at"]
  spreadsheet = open_spreadsheet(file)
  header = spreadsheet.row(1)
  (2..spreadsheet.last_row).each do |i|
    row = Hash[[header, spreadsheet.row(i)].transpose]
    product = find_by_id(row["id"]) || new
    product.attributes = row.to_hash.select { |k,v| allowed_attributes.include? k }
    product.save!
  end
end

Disclaimer: I'm not sure this qualifies as 'The Rails Way'. If someone has a more elegant solution, please post a reply.

Avatar

How would you go about adding the ID after the name if it is not unique?

Avatar

It turns out this is a stupid question...

I figured it out, in facebook.js.coffee just change the window location

facebook.js.coffee
  $('#fb-signin').click (e) ->
    e.preventDefault()
    FB.login (response) ->
      window.location = '/accounts/auth/facebook/callback' if response.authResponse
Avatar

Hi everyone, I got a question will trying to use omniauth-facebook with devise
I followed the video(just substitute twitter with facebook) and was able to get it work.
However, I would like to customize my routes a little bit, so this is what I did:

config/routes.rb
devise_for :users, path: 'accounts', 
           controllers: { omniauth_callbacks: "omniauth_callbacks" },
           path_names: { sign_in: "login", sign_out: "logout", sign_up: "signup" }

as I expected, in my url "users/..." was changed to "accounts/...", but when I tried to login via facebook, I got this error:
Routing Error
No route matches [GET] "/users/auth/facebook/callback"

after play around with it, I got around it by adding this line:

config/routes.rb
get '/users/auth/facebook/callback', to: redirect('/accounts/auth/facebook/callback')

It works fine now, but I think there must be better solution.
Is there any way to change the returned callback url from /users/auth/facebook/callback to /accounts/auth/facebook/callback ???

Thanks!

Avatar

Thanks I was just about to post my newby mistake. :)

Added both (@user, task) since I was indexing associations in the parent object's index (aka "task")

Avatar

As you are using nested resources you need to pass the task.

ruby
data-update-url="<%= sort_task_objectives_url(@task) %>"
Avatar

I did try to add one other tooltip to the price field
But it's not working. The first tooltip is displayed on the 2 labels !

ruby
    <div class="row collapse">
      <div class="small-3 columns">
        <%= f.label :name, class: "right inline", title: "Name of product", data: {tooltip: true } %>
      </div>
      <div class="small-9 columns">
        <%= f.text_field :name %>
      </div>
    </div>

    <div class="row collapse">
      <div class="small-3 columns">
        <%= f.label :price, class: "right inline", title: "Price in USD", data: {tooltip: true } %>
      </div>
      <div class="small-9 columns">
        <%= f.text_field :price %>
      </div>
    </div>

Any idea ?

Avatar

Routing Error, help :)

No route matches {:action=>"sort", :controller=>"objectives", :profile_name=>"Codemaster"}

routes.rb
  scope ":profile_name" do
    resources :tasks do
      resources :objectives do
        collection { post :sort }
      end
    end
  end
Terminal
sort_task_objectives POST   /:profile_name/tasks/:task_id/objectives/sort(.:format)       objectives#sort

I did everything else as stated in the video.
data-update-url="<%= sort_task_objectives_url %>"

I do have

class ObjectivesController < ApplicationController
  before_filter :authenticate_user!
  before_filter :find_user
  before_filter :find_task
  before_filter :ensure_proper_user
Avatar

Thank you! xml.atom(:link... was what helped me.

Avatar

Hello from 2013!

For rails 4 I am using the following code:

PostController:

def index
if params[:search]
@posts = Post.search(params[:search]).order("created_at DESC")
else

@posts = Post.all.order('created_at DESC')
end
end

Post model

def self.search(query)
# where(:title, query) -> This would return an exact match of the query
where("title like ?", "%#{query}%")
end

Index view:

<%= form_tag(posts_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search Posts" %>
<%= submit_tag "Search" %>
<% end %>

Good luck!

Avatar

Just an update - the issue appeared to be that the SMTP was not set up correctly. To rectify: you'll need to ensure your gem mail is set in your Gemfile. Next, create a setup_mail.rb file in config/initializers.

ruby
ActionMailer::Base.smtp_settings = {
        :address                        => "smtp.gmail.com",
        :port                                        => 587,
        :domain                         => "localhost:3000",
        :user_name                 => "whatever@gmail.com",
        :password                 => "password",
        :authenticaton => "plain",
        :enable_starttls_auto => true
}

Change to your smtp of choice. Next include in your

ruby
application.rb file: 
    config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.default :charset => "utf-8"

And then... magic.

Avatar

I realize this is 2 years (over) ago, but I thought I'd say I have the same problem. I found there was no .gitignore file so I created it with the README details, with no luck in an app I was working with called launchpage-rails. Seems to be a lack of config for the app.

Avatar

I'm having the same issue now. I get a "TypeError: Object [object Object] has no method 'best_in_place'" from the dom ready call. Did you ever figure it out?

Avatar

Does somebody has any idea how to use multiple dictionaries?

Avatar

I don't whether this is a problem of my own, just in case:
The content of og:type meta tag should be video.movie, instead of cinematron:movie.
I don't know the reason, the latter one just doesn't work for me.

Avatar

Just tried Metrical with a Rails 3.2.12 project and failed miserably. Found out that MetricFu had a new version, installed normally through Bundler and worked perfectly!

Avatar

For people having issues with transferring their data using taps, here is my fix:

https://github.com/ricardochimal/taps/issues/128#issuecomment-21049046

Avatar

Faye is really nice! However, in a lot of companies a lot of ports are blocked. So the setup breaks.

Is there a way to run my Rails app AND the Faye app on the same port (80)?

Avatar

Am I missing something, or shouldn't the Receipe and Comment model both have the following included (based on (154 Polymorphic Association episode)?

ruby
has_many :activities, as: :trackable

If not, then I must be missing the point of this polymorphic association.

Thanks!

Avatar

It seems that one of the things rspec-rails does is to add 'test:prepare' as a dependency to the test task. Commented it in the activerecord databases.rake to check and it's accountable for 2 seconds here.

Still cannot figure out where the other 2 seconds goes.

Avatar

Why running tests with rspec is faster than with rake? What's rake doing unnecessarily?

Avatar

this is a horrible name for the rails cast. Maybe "code coverage metrics" would make more sense.

Avatar

Thank you so much! I was stuck at this point and could not figure out what should I do. Thx so much!!

Avatar

Just a heads up. Cloudinary is expensive.

Avatar

Anyone know how I can do a spec test about a csv export? Thanks

Avatar

Using delayed_job gem with my rails 3 app on development working great but when I tried to use it on production using capistrano it gives me these error

script/delayed_job: Permission denied

I am using their method and I followed your method

https://github.com/collectiveidea/delayed_job/wiki/Rails-3-and-Capistrano

Avatar

I have an issue that each time I close my browser, I get signed out from my website and have to log in again on revisit. I do keep signed in at facebook.com...

Anyone got any idea what is going on?

Avatar

It seems like the Crop coordinates values are not being set.
This is what I get in my params hash after submitting:

"profile_photo"=>{"crop_x"=>"", "crop_y"=>"", "crop_w"=>"", "crop_h"=>""}, "commit"=>"Crop Photo", "user_id"=>"2", "profile_id"=>"2", "id"=>"7"}

Notice how all the crop_* are empty? What could be the reason?

I have both
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h

attr_accessible :crop_x, :crop_y, :crop_w, :crop_h
set.

Avatar

That's far from getting actually hacked though and there's no loss of customer information.

Avatar

Use
csv << product.attributes.values

instead of:
csv << product.attributes.values_at(*column_names)

Avatar

Anyone else have problem with the pluralization?

This is in my store.js.coffee

Raffler.Store = DS.Store.extend
  revision: 4
  adapter: DS.RESTAdapter.create()

DS.RESTAdapter.configure("plurals", entry: "entries")

But it is still looking for /entrys. Any thoughts?

Avatar

It's working, so far its been smooth sailing. I also set the data-turbolinks-track attribute to true.

application.html.erb
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
Avatar

I'm running into the same issue. Did you ever find a solution for this?

Avatar

If you're getting NoMethodError: undefined method[]' for nil:NilClass errors in the nginx compile:

Looks like as of Chef 11, you need to add depends 'nginx' to a metadata.rb file if you do an 'include_recipe'. (In this example, the medata.rb would be in the 'main' recipe directory.) See http://tickets.opscode.com/browse/COOK-2342

This is not necessary if you just add the recipe to your runlist in the node.json file.

Avatar

Hi all,

I have the same problem as http://stackoverflow.com/questions/15618099/multiple-delayed-job-processes-starting-same-job

I'm using: collectiveideas delayed_job 1.8.2, ruby ee 1.8.7 2012.02, pgbouncer + postgresql 9.2.

Regards

Avatar

Temp fix. while I'm trying to fix.

gem "rspec-rails", "2.13.2", :group => [:test, :development]

Bit of a cheat