RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

If anyone are looking for a way to exclude new tables from the multitenant migration (i.e. Only create the new table in the public schema). This solved the problem for me:

ruby
if ActiveRecord::Base.connection.current_schema == 'public'
Your migration 
end

Complete example

ruby
class UsersHasAndBelongsToManyRoles < ActiveRecord::Migration
        def change
                if ActiveRecord::Base.connection.current_schema == 'public'
                        create_table :roles do |t|
                                t.string :role
                                t.timestamps
                        end

                        create_table :roles_users do |t|
                                t.belongs_to :user, :null => false
                                t.belongs_to :role, :null => false
                        end

                end
        end
end
Avatar

for Rails 4.0, I think the turbolinks get in the way of allowing the header to refresh in some browsers. i.e. following this railscast when I clicked on the mobile/full site link, the css would not change unless I would do a page refresh in internet explorer and firefox (no problem with ipad though...). so I put in the body html tag data-no-turbolink in views/layouts/application.html.erb, and works great now. yepi.

Avatar

Also in Rails 4, remember to add the _destroy method to permitted params. Like this:

surveys_controller.rb
def survey_params
  params.require(:survey).permit(:name, questions_attributes: [:id, :content, :_destroy])
end

you also need to add it in the QuestionsController

questions_controller.rb
def question_params
  params.require(:question).permit(:content, :_destroy])
end
Avatar

@grammakov: You are a bloody lifesaver! Thanks for posting rails 4 compatible code.

Avatar

Maybe a silly question. I am using ubuntu 12.04 so the bootstrap for 10.04 just isn't needed. right? :) So far I just skipped that part because I figured it isn't needed. I just wanted to make sure I am not missing something by not using it.

Avatar

Thanks for the useful screencast! Just wondering two things.

  1. Is there any way to enable server side file size validation like: https://github.com/carrierwaveuploader/carrierwave/wiki/How-to%3A-Validate-attachment-file-size

  2. Client side resizing doesn't seem to work for me.
    https://github.com/blueimp/jQuery-File-Upload/wiki/Client-side-Image-Resizing

.js file
disableImageResize: false,
imageMaxWidth: 200,
imageMaxHeight: 200,

Does anyone have ideas?

Avatar

Don't use Active Record but create an SQL statement so the data insert is done in a single statement:

ruby
inserts = []

loop around your data do |line|
inserts.push("('#{Time.now}'::timestamp, \
'#{Time.now}'::timestamp,'#{line[0]}','#{line[1]}', \
#{current_user.id})")
end


sql_query = "INSERT INTO table_name (created_at,updated_at,attribute1, attribute2,user_id,) VALUES #{inserts.join(", ")}"

ActiveRecord::Base.connection.execute sql_query

This will take each array element and joins them with a comma to have a long single sql statement which is then executed. Make sure you don't have any single quotes in your incoming data. When using postgres you can replace them with two single quotes '' to allow them to be used as attribute values.

Avatar

in your controller:

def show
respond_to do |format|
format.html # show.html.erb
format.json { render json: @order }
format.pdf { render :layout => false }
end
end

Avatar

in your controller:

def show
respond_to do |format|
format.html # show.html.erb
format.json { render json: @order }
format.pdf { render :layout => false }
end
end

Avatar

Hi,

I tried

Roo::Excel.new(file.path, nil, :ignore)

But i get the following error.

wrong number of arguments (3 for 2)

Avatar

For what it's worth, if you want to use Torquebox (I heart Torquebox) but want the hands off style of infrastructure that you can get with Heroku it might be worth it to check out OpenShift from RedHat.

It's very similar to Heroku but a little more flexible for your configuration needs. It's worth noting that it's also in the same Amazon data center that Heroku resides in, so any 3rd party services or databases should be able to connect just as easily within the network.

https://github.com/openshift-quickstart/torquebox-quickstart

Avatar

its 2013 and this screencast is still actual

Avatar

The calendar is working great but when i deploy it (using Heroku), the current date is wrong. It's set to a day ahead. What's wrong??

Avatar

I am new to rails and i find these gems VERY VERY useful, thanks a lot!!!!

Avatar

Yes. That would be great. I am facing the same issue now on EC2. Do you know of any workaround?

Avatar

just tested it on my localhost

After you run
whenever --update-crontab

Go and edit the crontab (using crontab -e)
And replace production with development.

Note: You have to do this change every time you update the crontab.

Avatar

Same deal with me. In devise.rb, I just use:

config.omniauth :twitter, "key-xxxxxxxxxxxxxxx", "secret-xxxxxxxxxxxxxxxx"

Avatar

Try this:

ruby
  $('.brand_field').autocomplete
    source:  $('.brand_field').attr('data-autocomplate-source')
Avatar

I was able to get everything working, but I've discovered a quirk that I can't seem to figure out. When you click sign out, the fb-root div inserted by facebook.js.coffee.erb disappears. That means if you sign in again immediately, you'll be redirected to Facebook instead of getting the pop-up window. Reloading the page makes it reappear, but it would be nice to get it to behave consistently. I'm using rails 4 if it matters.

Avatar

How to use " More Like This " in ElasticSearch ?

Avatar

I was able to resolve this thanks to all that contributed to this

Avatar

This is great and works on local, but throws a 500 on production for some reason. I'm thinking it has something to do with what he mentions around ~08:09

"... In a real application you'll want to check the entire path to make sure that the parents are correct"

and then he talks about the shortcut method. Any ideas for what the non-shortcut method would be? I have a Node model I would like to be invisible as parent

Avatar

After creating a new migration, I always run:

ruby
$ rake db:migrate db:test:clone

which propagates any migrations to both development and test environments equally

Avatar

Great railscast, thanks!

However
gem install vagrant
only resulted in downloading vagrant version 1.0.7, which is way out of date
and gave me many mysterious errors.

the best way to install vagrant is from binary installer from:
http://downloads.vagrantup.com/

Avatar

Hi, I am newbie in rails.
I am trying to use ElasticSearch. But when I run rake db:setup, I don't see my articles any more. I guess the data is not loaded from seeds.rb. How can I solve that problem?

Avatar

Fuck Devise, Sorcery is way much cooler. Or easier perhaps. I don't want to go thru all the twinge I've been spending my whole afternoon figuring out how to work my way with Devise. And by the way, this is the simplest by far, I know it's outdated but it only gets better and improved by time. I'm good to go with this, 'Sorcery'.

Avatar

I've developed a method called group_date that is added to ActiveRecord::Relation.
You can read about it in this gist: https://gist.github.com/leods92/7164159

Avatar

Nevermind. I found where the recursion was happening...

Avatar

I added the following to development.rb:

config.assets.compile=true
Avatar

Have you tried calling #build as you mention? That seemingly innocent change somehow sends me into an infinite recursive loop; that is to say, link_to_add_fields gets called over and over again. I can't imagine how building the new object through the association would have this side effect. Here is the line of code before the change:

new_object = f.object.class.reflect_on_association(association).klass.new

after:

f.object.send(association).build

Any ideas?

Avatar

It's worth noticing that the code presented in the video causes unexpected timezone issues.
As timestamps are stored in UTC when you use SQL's date() function you'll get the date in UTC which can be different. E.g (consider -0300 the local zone) 2013-12-01 01:00 -0300 -> (stored as) 2013-11-30 22:00 UTC => (after date()) 2013-11-30 when you'd expect 2013-12-01.

Rails makes conversions itself when fetching a timestamp column but since you're grouping in SQL Rails cannot do anything about it.

I'm still searching about this and if I find a optimal solution I'll post here.

Avatar

My CSV file contains this type of symbol ᅠ and when i tried to import this file, the place of ᅠ is replaced with ? i.e, question mark and gets stored in database.

For example if this text is in my csv file "ᅠDinesh", upon import it gets stored as "?Dinesh".
Please help me out.

Avatar

sorry to bother you guys again. I got to the very end with everything working, but now receiving Stripe::InvalidRequestError in SubscriptionsController#create

my error highlighted this.

customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)

Avatar

Anyone have any tips for adding a foreign key? Say in Ryan's example, Factory had many Products, and one wanted to upload Products from a CSV that did not contain a foreign key to the Factory each Product belonged to. Thanks!

Avatar

Hammer, you need an equal sign after '<%', before 'submit_tag', like this:

'<%= submit tag "Import" %>'

Good luck!

Avatar

Hi guys,

great tutorial. I setup the subscription part and before integrating the stripe part, I want to add an association to the user. I have a user model with devise and want people to create a user account before being able to make a subscription. The problem is, that I am confused on how to get the user_id into the subscription. Can someone help?

Avatar

@tatumszymczak Were you able to get this working? I've got the same problem. Thinking it may be related to mobile browsers

Avatar

In linux/unix, almost everything is a file. Unicorn simply listens on a socket instead of a TCP port.

Avatar

Hello,

I have followed the Ryan's code and copy-pasted all the codes, but
the css style @media print{} is not affecting to the pdf file so I cannot hide the link. I have tested with Chrome, IE and Firefox. Link disappears when I click browser's print preview.

I am using rails 4.0 and ruby 2.0

Thanks for any help!

Avatar

Works good for me. Good package, thank you for sharing

Avatar
ruby
really nice broadcast