RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

as an easy solution try out the gem dejan/auto_html

ruby
include AutoHtml

def markdown(text)
  text = auto_html(text) do
    youtube ( width: 400, height: 250 )
    vimeo ( width: 400, height: 250 )
  end

  extensions = [tables: true, autolink: true, strikethrough: true]
  Redcarpet::Markdown.new(Redcarpet::Render::XHTML, *extensions).render(text).html_safe
end
Avatar

I'm having some trouble adapting railscasts with nested parameters using strong_parameters. I think this form creates an empty hash in answers_attributes and questions_attributes which cause problems on update for strong_parameters.

Avatar

Ryan, any thoughts about doing the "hide" request at the user level vs the browser level, such as using Redis to store the hidden state per user rather than cookies?

Avatar

Thanks Ryan. You seem to have this uncanny ability to put out new episodes that pertain directly to what I'm working on. I'm very new to testing, and I'm doing my utmost to learn it - it's like I can code without fear of breaking anything :)

Avatar

I solved this issue by modifying omniauth.rb. Basically you need to pass the path to your certificate, it's an issue related to openssl.

provider :facebook, Facebook::APP_ID, Facebook::SECRET, {:client_options => {:ssl => {:ca_file => "/opt/local/share/curl/curl-ca-bundle.crt"}}}

You can find more info and the link to the certificate in the following links:

https://github.com/arsduo/koala/wiki/HTTP-Services
http://martinottenwaelter.fr/2010/12/ruby19-and-the-ssl-error/

Avatar

This gem work in a Unix development enviroment ?

Avatar

How can we modify the omniauth_callbacks_controller if we want to add facebook authentications as well ?

Thanks.

Avatar

Really nice solution, thanks. I like these episodes the most, where you have the time to create a basic solution, test it and make it more user friendly (hiding + AJAX in this case). Plus I learned something about explicitly testing JavaScript today :)

Avatar

If a model needs to span schemas, it's usually easier to keep that model in the public schema or move it out to public if it's already namespaced. Then we just use the multitenancy strategy outlined in the previous video for those cases. It's possible to do both forms of multitenancy in a single app and having that flexibility is helpful. Also, we use apartment, which makes it easy to exclude models from being namespaced.

Avatar

We use the apartment gem referenced at the end, which has been working great for us.

  1. One feature that's particularly useful is that it has a way to provide a list of tables which should be be namespaced. So you would add Account or Tenant to that list, which saves you from having to remember which migrations need the extra check to see if the table exists.
  2. We don't use subdomain either. We store the current schema on the user and set the schema to that user's schema on every request made by that user.
Avatar

Any luck guys?
Having same issue with me as well. :(

Avatar

Great work again Ryan.

I starting using this on my latest project and it rocks. However beware of bug related to following relative urls.

Phantomjs is unable to follow a javascript redirect (i.e. window.location.href = XXX"
it throws a "Not allowed to load local resource" error.

AS a quick and dirty i just changed the relative url to full url in the affected code and that fixed the problem.

Hope that helps anyone that might encounter this issue.

Here is link to recent post with some specific issue tix

https://groups.google.com/forum/?fromgroups=#!topic/phantomjs/fVBd3kGOQh4

Avatar

Great screenscast.

I'm trying to adapt it to a blog app where the posts have a "related posts" feature.

So one post has many related_posts trough related_posts_association.

While the implementation works out of the box and I can create relationships in the console I can't find a way to make it work in the application.

In this railscast example the create action uses the "current_user" that is stored in a session by the auth system. In my example I would like to create the relationship in the _form view (new and edit) where I create a list of all the other posts, each one next to a checkbox.

Once the create or update button is pressed I would like to have also the relationships created.

Does anyone have an idea on how to implement that?

Avatar

like Samnang already said, it does not depend on QT.

For us it was quite a pain to get Capybara Webkit running because of the QT dependencies. I can also remember some compilation issues on CentOS.
So Poltergeist came as god sent. It is a nice small package that is "just working".

If you have Capybara Webkit running and are happy with it you probably do not need to switch.

Avatar

The carrierwave-direct gem implements the key methods through calling mount_uploader in the painting model.

Avatar

Ignore my post 2 posts up. Turned my config was working fine.. Didn't realize they weren't being updated with normal cap deploys so my server still had the old ones in etc/nginx/sites-enabled...

Avatar

I was having trouble with the add-apt-repository command on Ubuntu 12.10 - Command not Found, even after doing sudo apt-get install python-software-properties

This was the error:
sudo: apt-add-repository: command not found

This was the solution:
sudo apt-get install software-properties-common

After that it started working finally.

Avatar

The advantages I've noticed for poltergeist over capyabara-webkit as the javascript_browser for capybara are:

  1. Poltergeist's errors are a bit more detailed, and that can help with debugging.
  2. Poltergeist allows you set cookies
Avatar

I am trying to require the RMagick gem in a gem I'm making. I included it as a dependency in my gemspec file.

ruby
 gem.add_development_dependency('rmagick')

and require it before use in my lib/say_hello.rb

ruby
require 'rmagick'
module SayHello
  def self.hello
          "Hello my friend"
  end
end

After that i bundle install, gem bundle, gem install.

The gem work without the 'require rmagick' but i receive the following error otherwise.

/Users/****/.rvm/gems/ruby-1.9.3-p194/gems/say_hello-0.0.1/lib/say_hello.rb:2:in require': cannot load such file -- rmagick (LoadError)

Can anyone give me some advice on what I am doing wrong?

Thanks in advance..

Avatar

try it

_datatable.rb
link_to(image_tag('icons/edit.png', :title => "Edit " +page.name), edit_page_path(page)) + "  " + link_to(image_tag('icons/show.png', :title => "Show " +page.name), show_page_path(page)) + "  " + link_to(image_tag('icons/delete.png', :title => "Delete " +page.name), page, method: :delete, data: { confirm: 'Are you sure?' })
Avatar

I've been struggling for a while now getting multiple apps online on one server so I hope someone can help me...

I've followed this tutorial to the letter apart from a few version changes (Ubuntu Server 12.04, rbenv 1.9.3p194).

Everything works fine with one website but as soon as I add another website things start to go wrong. At first nginx doesn't start and gives an error that I need to encrease my server_names_hash_bucket_size: to 64 which it's kinda weird because my domainnames for both sites aren't that long.

After changing that nginx starts again and the first site works but the second one gives an error. What's also weird is that my first sites stays the default site accessible by the ip of the server and all the domains pointing to it even though I have listen 80 default deferred; commented out.

I use this nginx.conf for both site with only the server_name different ofcourse:

ruby
upstream unicorn-<%= application %> {
  server unix:/tmp/unicorn.<%= application %>.sock fail_timeout=0;
}

server {
  # listen 80 default deferred;
  server_name slimmetelefoon.info;
  root <%= current_path %>/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn-<%= application %>;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}
Avatar

I have used and been very happy with thoughbot/clearance but decided to follow this and roll my own since I want to use strong_parameters.

One thing I liked in Clearance is a signed_in? method so I added the following to application_controller. It works, just wonder if this is the proper place for it. I know it is very similar to current_user, but I seem to prefer this in certain places.

ruby
def signed_in?
  current_user.present?
end
helper_method :signed_in?

Here is an example of how I am using it. Note: I use (Haml)[http://www.haml-lang.orl) though indents aren't showing up below.


- if signed_in?
%li= link_to 'Sign out', sign_out_path
- else
%li= link_to 'Sign in', sign_in_path

Avatar

IMHO, we should really careful when deciding to use schemas fo multi-tenants. It is a one-trip ticket, you need to pay so much if you regrets.

I'm working on a app that uses postgres schemas for multi-tenants. And one of the most important requirement in this new release, is to share data between schema.
And for some reason, we cannot get rid of the schemas.

As a result, we need to switch between schemas dynamically according to the context. In some special cases, we need switch between schemas for several times in one request.
This really causes a lot of pain for us.

We try our best to move the common data into public schema, and leave the rest in schemas. Then we can use the search_path to enable us make query across schemas.

But the really pain is that, ActiveRecord caches the query result in its DbAdapter(aka ActiveRecord::Base.connection).
When schema switched, the cached is not cleared. Then you will get wrong data.
If you manually clear the cached, it hurts performance, and you might the association of your model works improperly, which strongly depends on the caches.

And another issue is that the schemas.rb, totally not work, since it cannot distinguish the schemas. So the gems depends on it, won't work properly, e.g. annotate gem.

At last, I wrote an utility called MultiSchema to help us to switch between schemas, hope it helps. You can find it in this post

Avatar

I just added PJAX to a (fairly big) application, and I'm wondering if I shouldn't rollback and add TurboLinks instead, since it's gonna be default on Rails 4.

Any toughts on that? Would you people still add PJAX to new projects?

Avatar

What would be really helpful is how to extend this to handle nested resources... Has anyone extended this to use nested resources and have some guidance to share?

Avatar

For what it's worth, if you are using the refinery-calendar with refinerycms and want to embed a calendar grid, the month-to-month navigation like this won't work, and will throw a routing error when you load the page:

=link_to "<", date: @date.prev_month

Instead, you'll need

=link_to "<", refinery.url_for(date: @date.prev_month)

which works just fine. Something of a quirk in Rails' mountable engines routing.

—Steve

Avatar

How do you adapt when authorization depends on a parent model when the resource is not a singleton? Say topics#index can only be accessed if the forum is public. The topics#index returns an array, not a singleton, so we can't call topics.forum.private?

Avatar

The path for the Jasmine Bundle doesn't work.

Avatar

Had the same issue and used the mini_magick gem and all is working fine now. Thanks for pointing that out.

Avatar

Bump

Same question: Can this approach be used for deploying a rails app configured for multiple root domains (e.g. domain1.com and domain2.com)?

If so, what changes would be required to make it work? I'm thinking about things like routing.

Thanks

Avatar

Same happening to me. switched to 2.13.4 but it doesn't have the cap deploy:web:[action] tasks also.

Avatar

Anyone know how to turn the textfield into a dropdown when a association is chosen as an attribute?

i.e. When you want to search by Category, is there a way for the textfield to turn into a populated drop-down?

Avatar

Hey Ryan, great episode. Have you tried capybara-webkit? How does PhantomJS compare to this gem? We've been using it extensively and it works pretty well.

Avatar

If you're using namespaces for your controllers, add a context for the view helpers as well. Like so:
if permitted_to? :create, :model, context: :namespace_model

Avatar

can I use this with has_and_belongs_to_many

Avatar

Did you find the solution of this issue?

Avatar

Can anyone explain why we have to do this so that it's threadsafe?

ruby
def self.current_id=(id)
  Thread.current[:tenant_id] = id
end

def self.current_id
  Thread.current[:tenant_id]
end

How about this?

ruby
def self.current_id=(id)
  @tenent_id = id
end

def self.current_id
  @tenent_id
end
Avatar

This question has been raised (but not answered) here: https://github.com/influitive/apartment/issues/1

Keep an eye on that issue, more information may be forthcoming.

EDIT: Issue is slightly different (although still worth reading for extra information).

Rails has built-in ability to use structure.sql instead of scheme.rb.

It is a rails config option (config.active_record.schema_format = :sql).

How it goes about it though is not something I have looked at yet but probably something you want to copy.

Avatar

for me Capybara-webkit is around 10 % faster than Phantom and capybara-webkit reports fewer false positives..