RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

how do you guys test that ApiConstrant ?

Avatar

Hey guys,

So in an attempt to get better at Ruby on Rails, I think it would be good for me to build the applications while watching the screen cast. I know Ryan has given us the before and after applications, but when i tried to start the server it showed just the basic information, meaning there was no data (texts or pictures). I am wondering if that is due to something i am doing or is it just not provided.

Thank you

Avatar

yup it is
use scope :name, ->{stuff}

Avatar

Hi Ryan,
How do i skip the devise login/logout updates in activities table?

Avatar

this should be so:

ruby
def incoming(message, callback)
    if message['channel'] !~ %r{^/meta/}
      if message['ext'].nil? || message['ext']['auth_token'] != FAYE_TOKEN
        message['error'] = 'Invalid authentication token'
      end
    end
    callback.call(message)
  end

we have to check for message['ext'].nil? because if we don't and someone sends a request without the auth token, the server crashes.

Avatar

Update regarding the registrations controller code I posted above - better to put the added code in "new" vs within "build resource':

class Users::RegistrationsController < Devise::RegistrationsController

def new
build_resource({})
resource.invitation_token = params[:invitation_token]
resource.email = resource.invitation.recipient_email if resource.invitation
respond_with self.resource
end

def create
super
end

end

Avatar

this worked for me as well! thanks! (using rails 4)

Avatar

Thanks for the $(document).on("click","#game_classes th a, #game_classes .pagination a", function() line Mizpah :)

Avatar

Just wanted to give the opposite feedback, thanks for showing how it is done in Coffee

Avatar

its not working with jquery.steps.min

Avatar

I have a problem, I cannot start server when using gem
This errors:
ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/routing/mapper.rb:191:in normalize_conditions!': You should not use thematchmethod in your router without specifying an HTTP method. (RuntimeError)
If you want to expose your action to both GET and POST, add
via: [:get, :post]option.
If you want to expose your action to GET, use
get` in the router:
Instead of: match "controller#action"
Do: get "controller#action"

Thank for help!

Avatar

same here mate, we are all into this problem, too bad the screencast didn't focus no the best solution for this approach

Avatar

In my case, the order of each gems in gemfile were wrong.

NG
gem 'sorcery'
gem 'mongoid', github: 'mongoid/mongoid'
gem 'bson_ext'

OK
gem 'mongoid', github: 'mongoid/mongoid'
gem 'bson_ext'
gem 'sorcery'

Avatar

It appears that config.exceptions_app = self.routes no longer works in Rails 4. I implemented custom error pages using this when my application was 3.2.13. However when I rewrote the application in Rails 4.0.0 it completely ignored my custom error pages. Thankfully I could move them to the public folder (no ruby code) even with localization. All I did was to create separate views for each locale (i.e. 404.en.html, 404.fr.html) in the pubic folder. No other changes were needed. When I get a 404 error for locale en the English error page displays. When I get a 404 error for locale fr the French error page displays. Maybe there is a way to use html.erb files in the public folder to access some of the information in this wonderful RailsCast episode.

Avatar

Hi,

I need to show Country and State on a form with values coming from two separate table with association set. I don't want to hide State drop-down instead populate it with values based on Country ID 1 which is populated in country drop-down by default. Presently when I load the form, State drop-down shows all the States grouped under Country. On Country change, State filters the values and populates correctly. Issue is on form load when Jquery Country change is not triggered. How do I achieve it?

Also, based on these selections, I want to show a nested model form - lets Users from particular city based on State selection.
Any guidance in the direction will also be highly appreciated.

Regards,
Gorav

Avatar

Can you please check the problem that I have on stackoverflow, in two days nobody was able to help me, I'm I don't know what to do anymore.

http://stackoverflow.com/questions/19856714/display-in-a-graph-just-unique-values-for-a-column/19857667?noredirect=1#comment29536506_19857667

Thank you

Avatar

It's tough to find beyond beginner level videos for a lot of this stuff. So I don't mind the JS at all.

There are about forty bajillion Rails episodes. Did you want all of those? :)

Avatar

The link_to_add_fields helper is called server side before the page is sent to the client.

If you look at the link_to_add_fields helper you will see that it generates a link_to at the bottom. This is the link that the client clicks on, not the link_to_add_fields helper method.

Avatar

If your getting:

"FATAL: role "postgres" does not exist"

Then it's likely you did what I did and put a - in your app name. Remove it.

Avatar

I had this problem, put an - in my app name and postgres kept erroring. Spent a day trouble shooting postgres by ssh until I noticed the string settings in the yml file.

Annoying the string format isn't in any documentation.

Avatar

I solved this, turns out running create_staging didn't actually deploy the app. I'm not sure why but I had to run a cold deploy to actually get the app live.

Avatar

First. I'm new to rails. Besides that, I could make it just fine. I even figured out how to show just a week, instead of the hole month.
But I need to create another table inside each day, with the hours of the day On the left, and the tasks for the respective hour on the right. Can anyone help me, pretty please?

Avatar

Include inside your Gemfile
gem "thin"
gem "faye"

Avatar

If you're using the nginx-unicorn-postgres template and you can't get this to work:

This is because this template doesn't include a load-balancer. (Unicorn only works on a single machine). In the railscast, haproxy serves as the load balancer and runs in the web instance and delegates to the app instances. haproxy though is not a part of the nginx-unicorn-postgres template. As I understand it, you can either

a) add haproxy to your rubber setup and include it in the web role or
b) if you're using AWS, you can use amazon's ELB (Elastic Load Balancer)

More info on this stuff at the following links.

https://github.com/rubber/rubber/issues/325
https://groups.google.com/forum/?fromgroups=#!topic/rubber-ec2/BxxlrZ1lEug

Also see Rob Bastian's comment above

Avatar

To note if you're using JRuby and converting your database to postgresql for Heroku; the pg gem won't install. Instead use the activerecord-jdbcpostgresql-adapter gem in your gemfile per this Heroku dev center link

Avatar

This Railcast really should include a warning **NOT ** to use rake db:setup if you already have a database full of rows that you don't want to lose! It's obvious for experienced devs but I can see how a beginner might misinterpret the instructions and cause himself big headaches.

Avatar

Antonio,

I am very happy I found this but I am having issues getting these tests to work for me. Anyway you can help me out with this?

Thanks!
nathan.biles@skipjacksc.com

Avatar

Since onbody update this episode,

[fog][WARNING] Unable to load the 'unf' gem. Your AWS strings may not be properly encoded.

If you got message like this, (You should, if you use the latest version fog) then you need

ruby
gem 'unf'

check this for reference

Avatar

Don't use dashes (-) in rubber.yml file because you will run into an error when you run cap rubber:create_staging.

    command finished in 413ms
failed: "/bin/bash -l -c 'sudo -p '\\''sudo password: '\\''  bash -l /tmp/create_master_db'" on production.foo.com
Avatar

Hey guys - i've been trying to do this for a couple of days now and haven't been getting anywhere. I have an advertisers view - i can see all advertisers i've created. I want to be able to select a date/date range at the top of the advertisers list and show advertisers for a particular day/date range. I'm sure there is a way to modify this to be able to do that. I've been using the created_at method and have even looked at by_star gem. not sure how to generate the different views though - I'm new in rails so still unsure of how everything works. Any help is appreciated. Thanks in advanced.

Avatar

I'm getting this, did you manage to solve it?

Avatar

There's something i'm having trouble understanding. How are the new objects being created in the link_to_add_fields helper method without making a call to the server?
As I understand, each click on the Add questions link calls the helper method. But how is the ruby code parsed when we are on the client side?
Also, I put a debugger in the function and saw it doesn't get called on clicking the link. Can anyone give a nice explanation on how this is working Client-Server wise? I'm a bit mixed up... :-/

Thanks!

Avatar

I'm using Rails 4 and haven't needed the attr_accessor, I've used attr_reader :author_tokens though and it works

Avatar

Hi Engin,

This might be too late for you, but I had this same issue and figured I would post the solution in case it is helpful to someone else out there.

For the Registrations controller, I had to create a "Users" folder in my controllers folder and then put the registrations_controller code below into my new "users/registrations_controller.rb" file (i.e. which inherits from the Devise registrations_controller). To understand why, take a look at the Devise registrations_controller "new" code on Github - essentially, the code that you added into your registrations_controller "new" section needs to be adjusted to use "resource" instead of "@user" and then it needs to run after the resource is built (ie. "build_resource({})"), but before the devise response code (i.e "respond_with self.resource").

For the controllers/users/registrations_controller.rb file:

class Users::RegistrationsController < Devise::RegistrationsController
  
  def new
    super
  end

  def create
    super
  end

  def build_resource(hash=nil)
    super(hash)
    resource.invitation_token = params[:invitation_token]
    resource.email = resource.invitation.recipient_email if resource.invitation
  end
end

For the routes.rb file:

...
devise_for :users, :controllers => { :registrations => 'users/registrations' }, :action => 'new' do
    get 'users/sign_up/:invitation_token' => 'users/registrations#new'
  end
...

For the invitation_controller.rb file:

class InvitationsController < ApplicationController

  def new
    @invitation = Invitation.new
  end

  def create
    @invitation = Invitation.new(params[:invitation])
    @invitation.sender = current_user

    if @invitation.save
      if signed_in?
        @invitation.update_attribute(:sent_at, Time.now)
        Mailer.invitation(@invitation).deliver
        flash[:notice] = "Thank you, your invitation has been sent."
        redirect_to new_invitation_url
      else
        flash[:notice] = "Thank you, we will notify you when we are ready."
        redirect_to root_url
      end
    else
      render :action => 'new'
    end
  end
end
Avatar

Hi,

How can I implement tokeninput in Rails 4 ?

With the strong parameters I can't use attr_accessor so it doesn't work.

Any idea ?

Avatar

This helped me as well, running Rails 4.0.1 and backbone-on-rails 1.1.0.0

Avatar

had to change the controller to

format.html { render request.path[1..-1] }

Avatar

Great screencast as always! Some reason, when trying to go directly to the url, http://localhost:3000/errors/404

Rails wants to look in errors/errors folder in the views.

ActionView::MissingTemplate at /errors/404
Missing template errors/errors/404, application/errors/404 with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee, :haml]}.