RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

This code works for me on Mac Os x, but when I deploy to Linux I receive a route error. Why it happens?

Avatar

Engines are just a way to package up part of a rails app, and then use it in multiple projects (either your own, or open-source, etc.)

For example, let's say you made a nice e-commerce system. If you package it up as an engine, other people can simple mount it into their rails app (say at the "/store" path), and voila, they now have a store! Very elegant.

Avatar

I too was wondering about the differences. Thanks for sharing!

Avatar

In most cases turbolinks is actually slower than manually loading content with ajax, as it loads the whole body, which is not necessary in many cases for many sites.

I made a gem ajaxify_rails (https://github.com/ncri/ajaxify_rails), which only ajaxifies the main content area. We use it in a large site, in fact it has been extracted from it. It has a fallback for IE, unless pjax and turbolinks. Happy to hear feedback.

Avatar

in our scenario we need to vary the number of associated records allowed. We have validation in the model i.e

accepts_nested_attributes_for :answers , :limit => 4,:allow_destroy => true

My question is . how do we prevent the Add Answers link from being rendered if we already have reached our limit ?

Avatar

in our scenario we need to vary the number of associated records allowed. We have validation in the model i.e

accepts_nested_attributes_for :answers , :limit => 4,:allow_destroy => true

My question is . how do we prevent the Add Answers link from being rendered if we already have reached our limit ?

Avatar

There is a gem twitter-bootstrap-turbo to make twitter bootstrap javascript plugins work on turbolink.

Avatar

Has anyone figured out how to do this if you're also using hstore?
I can't work out how to replace:

ruby
load Rails.root.join("db/schema.rb")

with a similar line that will load and execute the file db/structure.sql instead.

Avatar

1000 page hits and not even a second improvement with Turbolinks? For most people it seems like the extra hassle of refactoring code is not worth it. Heed Mr. Knuth's words in this instance, unless you absolutely need that extra 5%.

Avatar

Thank you.

'cap deploy:stop'
'cap deploy:start'

Fixed my route issues. Whenever I created a controller and updates everything, on my VPS it would not come up. After doing this then all my routes are working fine

Avatar

Thanks for this episode! Turbolinks is nice. It's like ajax loading on steroids. Probably would need lots of finessing to make it seamlessly work on a large site but I like the direction here anyway.

Avatar

Are the crop coordinates saved anywhere?

For example if you want to add a new style in future and reprocess it as the cropped version is that possible? Or would you have to save crop_x, crop_y etc. in the database.

Avatar

A co-worker of mine created a gem autoexec_bat which helps you modularize your javascript files. It helps you trigger the correct javascript based on e.g. controller action or component.

It really made the transition to turbolinks easy for us as we only had to change one file :)

Avatar

I've been trying out turbolinks and pjax several times and almost every time I disabled it after a while. It does feel faster and in fact is faster, but you'll have to come up with work arounds for some small problems every time. And you'll have this constant worry: will this still work for my IE8 users? Frankly, this approach just feels a bit icky to me, mostly because I feel less in control of what is going on.

Avatar

Have a read on my article Introducing Turbolinks for Rails 4.0 which a wrote a few weeks ago. It should answer your questions.

Avatar

How does this compare to PJAX?

Avatar

In order to make your code more concise, combining the "ready" and "page:load" listeners into one should work too:

$(document).on 'ready page:load', ->
  $(document).on 'click', '.edit_task input[type=checkbox]', ->
    $(this).parent('form').submit()
Avatar

It seems that when using the HttpHelpers in an Engine's routes file, I keep getting "uninitialized constant Controller". But, if I use a URL matcher in the Engine's routes file, it routes correctly.

For instance in the dummy application I have config/routes.rb:

Dummy app config/routes.rb
Rails.application.routes.draw do
  mount MyEngine::Engine => "/myengine"
end

In the engine, if the config/routes.rb look like:

Engine config/routes.rb
MyEngine::Engine.routes.draw do
  get "things/index"
end

... then, rake routes in the dummy application looks like:

rake routes
$ rake routes
Routes for MyEngine::Engine:
  things_index GET /things/index(.:format)  things#index

... and when I try to access http://<host>/myengine/things/index, I'll get an uninitialized constant ThingsController.

However, if I change my Engine routes to:

Revised Engine config/routes.rb
MyEngine::Engine.routes.draw do
  match "things/index" => "things#index", :as => "things_index"
end

... then, rake routes in the dummy application looks like:

rake routes
$ rake routes
Routes for MyEngine::Engine:
  things_index GET /things/index(.:format)  my_engine/things#index

... and when I try to access http://<host>/myengine/things/index, I get the expected view.

Btw, rails g controller things index will generate a route using the HttpHelper method (not the matcher).

I googled for any corroborating accounts, but didn't see any. So, I'm not sure if this is just something I'm doing wrong. Or, if maybe this is because engines are fairly new.

I'll also post this to stack overflow to see if there's any help there.

Avatar

I have recently created plugin for AA, which helps to easy and dynamicly change sidebar position to left or right. I put sources here https://github.com/Fivell/active_admin_sidebar

Avatar

Hi Ryan,
these screencasts are really great. And this way you demonstrate is "the way" to go.

Still, being new to this (that being the reason I watch), it is a critical time in terms of adopt/not adopt and the instructions being outdated does not help.

I would suggest a notice right up top pointing this out, maybe to David Fishers post about how the FactoryGirl syntax works now.

Still, thanks also for all those other great intros,

Torsten

Avatar

Seems to be fixed in the github master. Meanwhile just switch back to 2.13.4 or use the master.

Avatar

Cool - about three weeks after this Railscast Ryan did go into the Russian doll technique, but actually did DHH's post one better. DHH outlines a manual versioning system for fragment content, since fragments can change without updating the models referenced within. As Ryan points out, that can get tricky.

In his Cache Digests Railscast, Ryan outlines how to hash and cache the full contents of a block, including the template and model contents. This eliminates the need for manual cache versioning and resolves some of the gotchas of the previous technique.

Avatar

I have the same problem, somebody can help me ?

Avatar

I'd better use

zsh
cp config/application{,.example}.yml

instead of

zsh
cp config/application.yml config/application.example.yml
Avatar

I usually deploy projects using a configuration management system such as Chef or Puppet. I create templates for the settings files and the templates are populated using data pulled from encrypted data bags on convergence.

Avatar

I like what you did here. This was bothering me too

Avatar

How would you destroy a comment?
I get:

NoMethodError in CommentsController#destroy
undefined method
comments' for #Comment:0x000000030aae58`

comments controller:

ruby
 def destroy
    @comment = @commentable.comments.find(params[:id])
    @comment.destroy
Avatar

Is it possible to have models with relationships spanning schemas? Like if a you had a user in a tenant schema and wanted to associate them with the tenant model in the public schema.

Avatar

+1 for a followup cast

as always - awesome work Ryan!

Thanks a lot!!!

Avatar

Figured out the answer to my problem....I forgot to override @current_resource

controllers/users_controller.rb
def current_resource
    @current_resource ||= current_user
end
Avatar

@rayn ,,I think now both gems has problems ,,,I found a jquery plugin which is more easy ,,just need to be used in rails

https://github.com/vdemedes/joconut

Avatar

Does anyone know how to change the file_field when dealing with nested attributes? The main table is users. each has_many images. The application works fine for uploading a single file (using carrierwave).

ruby
views/users/new.html.erb
<%= f.fields_for :images do |z| %>
<fieldset>
<p>
    <%= z.label :image_title %><br />
    <%= z.text_field :image_title %>
    <%= z.hidden_field :image_priority, :value => '1' %> 
  </p>
  <p>
    <%= z.file_field :image_location %>
  </p>
</fieldset>
<% end %>

The railscast says you should add a "multiple" option to file_Field and then "hardcode" the field name. So somthing like this:

ruby
<%= z.file_field :image_location, multiple: true, name: "images[image_location]" %>
Avatar

I've been out of Rails for about a year and just trying to get back up to speed on what has changed and new approaches. I've written a couple small applications that are deployed from home (small audience, like my golf group), but I never completed what got me started on Rails - trying to rewrite an application that was and still is in 4D (aka 4th Dimension) using Active4D as the web access to 4D.

While I am retired, it still bugs me that I never got that conversion completed and occasionally waste a little time thinking of new approaches. This screen cast and the multitenecy got me thinking about yet another approach and is close to what I did years ago for my golf group maxwell.golfgaggle.com, playing with subdomains for groups and accounts (this would have helped at that time).

The application is to support AIDT aidt.edu which is an Alabama State agency that is part of Alabama's economic development team. If short it recruits and screens prospective employees for Alabama businesses (think Mercedes, Honda, Hyundai and many other smaller companies who have located in Alabama).

What I've potentially found this week is that routes, namespaces and subdomains are our friends for solving certain kinds of authentication and authorization problems. The web interface basically had two virtual hosts. jobs.aidt.edu the external site that candidates for job training leading to employment could apply, and an intranet application used to manage parts of the process over the web. The goal was to expand this into several other virtual hosts to enhance the process (project. to allow company users to interact with the process for their company project, instructor. to allow contract instructors to do some things, and then admin. to shield some stuff from the employee users. The above links are to the Active4D site, Rails is still just a thought and AIDT's remaining staff is undermanned - why a try to push them sometimes.

I threw off Devise several years ago and went with Simple Authorization (scratch) and CanCan. While it states that CanCan does not work well with namespace, I think I can get around it because namespace does the majority of the authorization by separating the database. The subdomain/namespaces jobs, project an instructor have limited access to just a few models and the controllers control the access. If they are logged in, thats all they can do. Admin just protects some stuff, but the intranet is pretty wide open and lends itself to CanCan type authorization.

My current approach is:

User is polymorphic as loginable to Citizens, Employees, Contacts and Instructors (maybe others) and only Citizens (jobs namespace) can register, all others are invited.

Most routes are constrained by subdomain

ruby
  constraints :subdomain => 'jobs' do
    namespace(:jobs, :path => '/') do
      resources :citizens, [only ...]
      resources :jobs, [only ..] 
      #etc    
    end
  end
  
  constraints :subdomain => 'admin' do
    namespace(:admin, :path => '/') do
      resources :users
      resources :employees do 
        get 'invite'
      end
      resources :citizens
        #etc
    end
  end
  
  
  constraints :subdomain => 'info' do
        #not namespaced intranet site
        #most models available
  end

Have not set up CanCan yet, but it appears that Read Write Manage, etc will take care of most requirements. Non CRUD actions like 'invite', 'drop', 'progress' can also be handled.

This really applies to several screen casts but just thought I'd stick it here.

Ryan, thanks for your work. I'll never be a Rails expert but you certainly open my eyes to different ways of solving problems that sometimes seem unique.

Avatar

Hi Ryan,

I have seen your one of the video of face book in which you use a localtunnel gem.I do the same process and localtunnel generate a URL for me.

But i am confused because when i enter that URL in my address bar then after some time error occur.
I read about localtunnel gem , but none of that is useful for me.

I have some questions:-

Q Can you tell me how to use localtunnel ?
Q Is that URL is Accessible when i enter it in my address bar ?
Q If not accessible than what is a use of it?
Q If that URL is not working than how it be public available ?

As per my knowledge We can give our ip address with port number and any one can use it . Than were we are using localtunnel gem ?

Hope you understood the situation ?

Thanks

Regards
Fahim Babar Patel

Avatar

My sample app here, running the Ryan's source downloaded from this episode: http://jquery-upload-giang.herokuapp.com/paintings

Avatar

Thank you, thank you, thank you for this comment!

I was trying to index multiple columns without seeing any kind of performance gains using the pg_search gem. Sped up my full text query of 2000+ records from 1700 ms down to 60 ms.

Avatar

The progress bar doesn't seem to finish when file uploaded. I'm trying to upload a single file.

Avatar

Just pushed an update that fixes IE8/9. For those that what to implement it as a fix to what Ryan provides its a rather small change to the helper method, see what I changed here:

https://github.com/waynehoover/s3_direct_upload/commit/b9d4e54e3a27780ec340afba8bf2df8d60e14990

Avatar

I am getting the same error. I'm just using raw Faye, not private pub though. Have you found a solution?

Avatar

+1 for an updated railscast on Cucumber.

Avatar

It's not a solution. You still losing features like :prefix and searching multiwords trought multicolumns. Searching results show you "Lex Luthor" where both words are in same column. But not, when "Lex" is in one and "Luthor" in another. Ryan show faster soulution, but with limitations.