RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

What an awesome screencast. This also works with HABTM models as well! I wasn't expecting it to, but it worked :) Thank you!

Avatar

Hey, I've released sidekiq_mailer. A gem that handles asynchronous mail sending using sidekiq.

Avatar

Remember that you're sending back a "js.erb" view template. This just renders out the _task or form. So if you already had error message handling, it will just work. Try it!

Avatar

+1 Very efficient. I hope Ryan can update the log to include this. This comment is very useful, and prolly missed by a lot of people.

Avatar

netwire, I am using Mongid as well and I can not get the bootstrap pagination to work. Any ideas?

Avatar

It would be nice to have a part 3 or something which explains how this thought process carries over to CanCan, since you are the one who wrote it in the first place :)

Avatar

static page linking

ruby
require 'subdomain'
constraints(Subdomain) do
  root :to => 'staticPage#staticPage', :as => 'subdomainRouteName'
end


<%=link_to "Subdomain", root_url(:subdomain => "subdomainRouteName") %>


<%=link_to "Home", root_url(:subdomain => false) %>

Great screen casts, thank you!

Avatar

devise_rpx_connectable doesn't seem to work with latest versions of Rails/Devise.

Avatar

This is nice it solved my problem

Thanks

Avatar

I was getting the same and resolved it by including a redirect on success in the controller, as in the screencast.

@uploader.success_action_redirect = user_profile_url

Avatar

Thanks Ryan. This is so cool, I have never thought of using struct in Ruby/Rails, only in C, C++.

Also, unrelated to this. Few months ago, you sent out an email about either cutting the number of episodes/week or raising price. I can tell you that, with no hesitation, I would pay triple, even for now. Your episodes are clear, your teaching is lucid and most important of all, I like learning from you. I am more eager to see your new episodes than the new Family Guy episodes. Really thank you and keep up the outstanding work.

Avatar

Not sure if this is possible. The CORS configuration on S3 would have to allow origins from every single user that is using your API. The examples in this screencast upload files directly from a user's browser straight to S3. The file never goes through the rails application, only metadata about the file does.

You could get fancy and create some sort of registration process, where by the user of the API during the registration process enters the domain (origin) they plan to upload from. Or maybe they have a admin screen where they can change it out. AWS does expose an API where you can change settings on your S3 service. So when your users are in the admin screen, and they change the domain, then your rails app uses the S3 API to change the origin (or add) that the user entered. I hope I explained that well. I'm just spitballing here, never done this myself.

Avatar

Since the main check is in the application controller the permission logic feels like a controller to me, why not permissions_controller.rb in the controllers directory? - it just doesn't feel like a model to me. Is it just me?

Avatar

Anyone else have this issue?:

undefined method `user_signed_in?' for #<#Class:0x007f146c06a3d8:0x007f146c067700>

I'm sure I've done something wrong but...what?

Avatar

The profile image is tweet.user.profile_image_url

Avatar

I just began Rails 3 weeks back and for a beginner like me I cannot thank you enough for such detailed help on Authentication, with minor changes in Rails 2.3.8 I have implemented this successfully for my work, thank you.

Avatar

the tag_cloud from scratch isn't working here. Is it may be because I'm using postgresql as a database?

Avatar

So I am having trouble linking to static pages. I am trying to do something really simple, subdomain would be a static page and then link between several other static pages within that subdomain. I have it working with subdomain.domain.com/ and subdomain.domain.com/static-page, but how do I link to that?

Avatar

This is just a quick way to get a "user" attribute. It also saves me from having to define the "initialize" method here.

Avatar

Spork gem doesn't support Rails directly anymore. Now, you should use spork-rails:
https://github.com/sporkrb/spork-rails

Avatar

Thanks for the tutorial Ryan. Has something changed with the Asset Pipeline in Rails 4 beta that I'm not aware of? After following your screencast, when I visit http://localhost:3000/jasmine, none of my assets are being loaded. The JavaScript and CSS tags are referencing things like http://localhost:3000/jasmine/assets/jasmine.js?body=1 instead of http://localhost:3000/assets/jasmine.js?body=1 Do you have any ideas? I've been reading through the source of both Jasminerice and Rails and can't seem to figure this out. Thanks!

Avatar

Thanks Ryan for this !
I notice a little "cheat" on the video at 2:02 : on your console
>>require "java"
=> true
become false at 2:06. That mean that you had an unexpected thing during recording the screencast. That's normal in the real life. And if a mistake is made by you, you can be sure that many of your followers (at least me ;-) ) will do the the same. So why not explain it ? Non working things teach us many (if we get explication).
I'd rather have the notice about a frequent issue/mistake. I tell this as feedback to improve your RailsCasts that are already awesome. I'm not complaining ;-)

I saw that because I've an issue with require 'java' that raise an error (No such file to load -- java) with normal Ruby and return false with Jruby... (and I'm trying to make my app work on both platforms.) Anyone can help me saying more about that commande please ? Thanks.

[EDIT] I realized that if I've a false return with jruby, that is because java is already loaded by my app as long I use the rails console. Indeed my app has a presenter that require java.
For you it might be different : the record in pause then you have written some more lines, made some try and rewrite first lines before restart record and as it was not the first time it was required, require 'java' return false this time... [/EDIT]

Avatar

Great Job, Ryan!

Can you please tell me why or what is the advantage of having your Permission class inherit from a Struct (class Permission < Struct.new(:user)), why a struct?

Avatar

I am sure you will put this in Part 2, but I am implementing this in an app right now and needed a way to ensure they were an admin for the associated member they were viewing.

ruby
if !current_permission.allow?(params[:controller], params[:action], params[:id])

Then you can use the Id to fetch and check on an item if needed.

ruby
return true if controller == "projects" && action.in?(%w[edit]) && Project.find(id).user == user
Avatar

Hi,

i want to tell validates_attachment_content_type to only allow the extensions ipa, mobileprovision and plist but i am not sure what MIME-Type i have to set. I have tried ['application/.ipa'] but this is not working.

Any ideas?

Regards,
buk

Avatar

Hi, thanks for this Screencast.

I have some troubles installing postgresql.

When typing in:
apt-get install postgresql libpq-dev

I get: could not find libpg-dev

Does somebody knows the problem?

Thanks

Avatar

To fix the psql issue, make sure the postgresql version you installed is not 8.4 but 9.1

Avatar

fixed it by doing

brew uninstall node
brew install node

Avatar

when I try to start the forum-after application (with rails s), I'm getting thie following error. Does anyone know what's wrong, as in how to fix it? I did bundle install and it's not changing anything. Please note that I recently uninstalled MacPorts and the error might be associated with that, but i still don't know how to fix it.

ruby
Completed 500 Internal Server Error in 1479ms

ActionView::Template::Error (dyld: Library not loaded: /opt/local/lib/libssl.1.0.0.dylib
  Referenced from: /usr/local/bin/node
  Reason: image not found

  (in /Users/michaeljohnmitchell/Sites/authorizationPart1/forum-after/app/assets/javascripts/sessions.js.coffee)):
    3:   <head>
    4:     <title>Forum</title>
    5:     <%= stylesheet_link_tag    "application", media: "all" %>
    6:     <%= javascript_include_tag "application" %>
    7:     <%= csrf_meta_tag %>
    8:   </head>
    9:   <body>
  app/views/layouts/application.html.erb:6:in `_app_views_layouts_application_html_erb___3636075050981736840_2167808160'


  Rendered /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.5ms)
  Rendered /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.2ms)
  Rendered /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (17.1ms)
^C[2012-10-08 23:48:50] INFO  going to shutdown ...
[2012-10-08 23:48:50] INFO  WEBrick::HTTPServer#start done.
Exiting
Avatar

Members is a method that comes with Struct, which Permission inherits from in Ryan's example. http://ruby-doc.org/core-1.9.3/Struct.html

Avatar

according to my Pry console. Permission class has a class method called "members" which return :user. I don't see where this method is defined...Does anyone know?

Avatar

Thanks for posting the link to Joshua's cookbooks. It looks like 37Signals cookbook has been removed from their Git account, or made private.

Avatar

There is a missing step here, and in the example code.

When uploading images to S3, you want to specify the Content-Type, otherwise S3 will just default it to "application/octet-stream", which can cause your browser to throw warnings when you retrieve the file.

This caught me out, because Content-Type is listed in the multi-part form request as a component of the "file" input, however crucially S3 will NOT look at that. You need to pass it again.

In my case, using jqueryFileUpload as Ryan did, I have

<input id="content-type" name="Content-Type" type="hidden" value="" />

and

add: (e,data)
  ...
  data.form.find('#content-type').val(file.type)
  data.submit()
Avatar

The if-block method couples the find_commentable to the thing that is commentable. If you add another one tomorrow then you have to go add another case. While that's ok in an app with an app level controller, if you have something like a mountable engine, you'll have to pick a convention and stick with it, sans-special knowledge of 'all things commentable.'

Avatar

Someone know how put this work using nested_forms, for exemple products upload many photos in the products new form?

Avatar

These comments are more than 6 months old but I came across the same problem and the fix is to stop and start the unicorn service separately after cap deploy

cap deploy:stop
cap deploy:start

unicorn_init.sh seems to have an issue for a restart with the following:

restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
run "$CMD"
;;

The unicorn service will not point to the latest release and recognize the new gems you might have added to the Gemfile since your last deploy.

Hope this helps.

Avatar

Oh, very cool! Never identified that as a potential issue, but I can see that it definitely could be. Yay rspec goodies!

Avatar

Yeah, you should find the following at the bottom of your spec/spec_helper.rb file:

ruby
# Run specs in random order to surface order dependencies.
config.order = "random"

This has been part of rspec for several months now, you probs just didn't notice it.

Avatar

Great episode! Anyone know what that Randomized with seed 43829 line is? I haven't encountered that with Guard or RSpec before...

Avatar

the .page() and .per_page() are part of will_paginate. If you use kamanari, it's .page() and .per(). I'm not familiar with total_entries, but I use Mongoid and I can use .size

Avatar

Folks have asked about combining Devise and other service providers, and there are great turorials (if somewhat dated and/or buggy) out there: github.com/RailsAps

I used the example on rails3-mongoid-omniauth and combind with the railscasts to get Devise, omniauth-facebook and mongoid up and running for myself. Here's a pastebin devise, omniauth-facebood and mondoig

I only posted relivant parts in users.rb which required a deviation from Ryan's

ruby
def self.new_with_session(params, session)

method so things would work with mongoid.

The other area of change is in the authentication_controller.rb

ruby
def all

went through some changes so that we could allow username/password registrations and logins, registrations and logins via facebook and to allow account linkage of existing accounts with 3rd party providers.

Also had to add a method:

ruby
def create_new_omniauth_user(auth)

in the controller, again to appease the mongoid gods since the Authentication can't be saved before the user record.

I went about making a authentications "table" to store the different 3rd party credentials in so users could use multiple providers to login or sign up with, however the only one I've implemented is facebook, so there may be changes you'd need to do in the user.new_with_session method to work with the omniauth.auth hash data.

Avatar

Any ideas how to overwrite the to_param method? Overwriting it in /app/models/tag.rb just doesn't work in urls, though it works in console when invokung Tag.first.to_param

What i mean is instead of urls like /tags/foo%20bar i would like to have urls like /tags/foo-bar

Avatar

There's a need to create a plugin to call .remove() on Update button. So maybe there's not much difference what method to use here anyway.

Avatar

I've deleted your questions and duplicate comments per your request :)

Avatar

I haven't touched RoR in a month and getting rusty, took a small break came back to computer and it hit me, the problem was with a haml conversion from html:

WRONG WAY

ruby
#tag_cloud
  = tag_cloud Post.tag_counts, %w[css1 css2 css3 css4] do |tag, css_class|
    = link_to tag.name, tag_path(tag.name), class: css_class

RIGHT WAY

ruby
#tag_cloud
  - tag_cloud Post.tag_counts, %w[css1 css2 css3 css4] do |tag, css_class|
    = link_to tag.name, tag_path(tag.name), class: css_class

Can delete my questions to clean up this railscast =)