RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

hey guys. I spent some time rooting around and it's possible to do the same on linux...but it's not elegant.

Here's a post I wrote on how to do it. Or, at least it will point you in the right direction.

Avatar

Excellent Railscast, as always!

That said, would anyone be willing to share opinions on when it's appropriate to use these frameworks (Angular, Backbone, etc.) vs. the traditional Rails/AJAX way of doing things? I image the benefit has to be pretty tremendous to undertake the learning curve but it's not apparent to me.

For example, would building the app the traditional way be suboptimal? And why?

I really appreciate the guidance! I'm allocating my excess capacity toward becoming a better Ruby programmer but would be willing to divert resource toward learning a new .js framework if that's a more prudent direction.

Avatar

thanks very nice... I think will take this alternative over Backbonejs ... very nice

Avatar

Everyone just a note.... In some of the firefox browsers the headers get passed as HTML.

To fix this Ajax issue just respond_with :json, :html

Avatar

Running "god log mydaemon" only gives me "Please wait..." and nothing shows up. What could that be?

Avatar

Fantastic jump start (as always); mirrors all the lessons I had to learn myself over the last couple weeks.

I highly recommend https://github.com/tpodom/angularjs-rails-resource, especially the nested_urls branch, as an alternative for synchronizing with rails.

Avatar

I get the error uninitialized constant Model::Excel. Anyone else have this problem?

Avatar

Changing jquery-rails version to 2.1.4 seems to have done the trick.

Avatar

I have this same issue which cropped up when I tried adding the progress bars.

Avatar

Did you find help for this? I, too, would like to copy the record above to create a new record - like an Excel sheet...

Avatar

This is perfect. I needed to export some data as CSV, and all the previous go-by's I had seen built the CSV content in the controller. Yucky! This moves it out to the view template where it should be, and frees you up to make the respond_to handler look nice and pretty. Good work!

Avatar

Has anyone got this working with jQuery 1.9.0?

I did an gem update on jquery-rails and now getting the following error

Uncaught Error: Syntax error, unrecognized expression: ...

Sizzle.error

Avatar

How could I accomplish this sort of thing with a single model? I want to create several records at once with an "Add Another Record" button, but I'm only using a single model, not nested...

Avatar

I had this issue. I develop on Windows 7, and certain gems have windows-specific versions. I went into my Gemfile.lock and removed all "x86-mingw32" in the gem version numbers. After commiting the changes and deploying again, it worked.

I also had this problem with postgres and the pg gem.

Avatar

Answered my own question. Rewrote the unicorn_init.sh file on a Unix machine instead of my usual Windows box.

Avatar

Did you ever find an answer to this? I'm can't seem to beat this one.

Avatar

Doesn't work if I use respond_with (db_runtime is always 0 after POST request)

Avatar

Hi Ryan,
Thanks for sharing all your free railcasts.

I have a question:-
Since you can intercept the incoming request and process the Ability why not process the rendered page and automatically filter out all the links and actions that would be routed to incoming resources that are protected ?

Any suggestions how to do this ?

Best regards ozpoz

Avatar

So let's start by saying Thanks, Ryan, great cast, really helpful and all that jazz.. (I mean it though)
Just one difference I'd like to share that I encountered on a fresh Ubuntu 12.04 VPS installation following the instructions of this screencast. After installing postgres, and adding a project specific user with password, I also had to modify the pg_hba.conf (/etc/postgresql/9.1/main/pg_hba.conf) file to enable the md5 authentication method, so that rails is able to connect to it with user and password specified in database.yml. Details are easily googleable, just wanted to point it out..

Avatar

Nice video like the gems i just got some problem. I can't find how to setup urlhandler for sublime text 2 on linux.

Avatar

Scoping is slightly different in rails 3.2 as shown below:

ruby
scope :with_role, -> { |role| { :conditions => "roles_mask & #{2**ROLES.index(role.to_s)} > 0" } }

Essentially, lambda is replaced by -> and named_scope by scope in rails 3.2

Avatar

Here's a simple multi-rating system for Photos based of Ryan's videocast. It is simple: just pass the value as the params[:type]: vote_photo_path(photo, type: "1"). Aalso using twitter/bootstrap/font_awesome for the vote icons

vote:
Notice the current_user cannot vote on their own photos;

And I included a 'status' attribute for 'Approved' or 'Denied' the display/hide the photo from public viewing.

Automatic Quality Control: Notice the automatic status change to 'Denied' is ONLY after 10 votes and with an average value less than 3.

vote_range:
This is in the application_controller.rb because I use the reputation system on several models: Ads, Photos, Profiles.

vote_reset:
This allows for resetting the values.

voteable?:
Allows control of who can vote

models/photo.rb
  has_reputation :review_photos, source: :user
  scope :public, where("status = ?", "Approved")
models/user.rb
  has_reputation :review_photos, :source => { :reputation => :review_photos, :of => :photos }
controllers/photos_controller.rb
def vote
      
    @photo = Photo.find(params[:id])
    
    if current_user
        
      if @photo.user_id != current_user.id
      
        value = params[:type]
  
        if vote_range?(value)
          @photo.add_or_update_evaluation(:review_photos, value, current_user)
        end
        
           # if low score on photo - automatically update status to "Denied" to hide from "Public" - @photos = Photo.public

        if @photo.reputation_for(:review_photos).to_i < 3 && @photo.evaluations.count >= 10
          @photo.status = "Denied"
          @photo.save
        end

      end
      
    end

    respond_to do |format|
      format.html { redirect_to :back, notice: "Thank you for voting" }
      format.js
    end
  end
 
  def vote_reset
        
    @photo = Photo.find(params[:id])
    
    if @photo.present?
      
      evaluations = @photo.evaluations
      evaluations.each do |eval|
        eval.destroy
      end
      
      reputations = @photo.reputations
      reputations.each do |rep|
        rep.destroy
      end
      
      @photo.status = "Approved"
      @photo.save

    end
controllers/application_controller.rb
  def vote_range?(value)
    value.to_i.between?(-3,10)
  end
  helper_method :vote_range?

  def votable?
    if user_signed_in?
      if add - your - criteria - for example...
        if current_user.status == "Approved"
          true
        end
      end
    end
  end
  helper_method :votable?
views/photos/_vote.html.erb
<div id="review_photo_<%= photo.id %>" class="center star_bar">

    <%= photo.reputation_for(:review_photos).to_i %> points | avg score <%= photo.evaluations.average('value').to_i %> |
ranked <%= photo.rank_for(:review_photos).to_i %>


        <% if votable? %>
                <%= link_to vote_photo_path(photo, type: "-3"), method: "post", remote: true, :title => "-3 point" do %>
                  <i class="icon-thumbs-down"></i>
                <% end %>
                <%= link_to vote_photo_path(photo, type: "1"), method: "post", remote: true, :title => "1 point" do %>
                  <i class="icon-star"></i>
                <% end %>
                <%= link_to vote_photo_path(photo, type: "2"), method: "post", remote: true, :title => "2 points" do %>
                  <i class="icon-star"></i>
                <% end %>
                <%= link_to vote_photo_path(photo, type: "3"), method: "post", remote: true, :title => "3 points" do %>
                  <i class="icon-star"></i>
                <% end %>
                <%= link_to vote_photo_path(photo, type: "4"), method: "post", remote: true, :title => "4 points" do %>
                  <i class="icon-star"></i>
                <% end %>
                <%= link_to vote_photo_path(photo, type: "5"), method: "post", remote: true, :title => "5 points" do %>
                  <i class="icon-star"></i>
                <% end %>
                <%= link_to vote_photo_path(photo, type: "10"), method: "post", remote: true, :title => "10 points" do %>
                  <i class="icon-heart"></i>
                <% end %>
        <% end %>

</div>
views/photos/vote.js
  $('#review_photo_<%= @photo.id %>').hide().after('<%= j render("voted") %>');
views/photos/_voted.html.erb
<div id="review_photo_<%= @photo.id %>" class="center star_bar">
        <% if !user_signed_in? %>
                Please Login to Vote
        <% elsif @photo.user_id == current_user.id %>
                Don't Vote on Your Photos
        <% else %>
                Thank You!
        <% end %>
</div>

I hope this helps

Avatar

Can you pls elobarate how should i use this

Avatar

I've used it do have resources like:

/orgaizations/lend-a-hand/needs/pillows

Works nicely.

Avatar

I've looked into the at_exit callback, and thought of this:

app_builder.rb
class AppBuilder < Rails::AppBuilder
  def initialize(generator)
    super(generator)

    at_exit do
      postprocess
    end
  end
  def test
    gem_group :test, :development do
      gem 'rspec-rails'
    end

  end

  def postprocess
    generate 'rspec:install'
  end
end

This way, you have a single method for postprocessing which is called after bundler.

Avatar

Erwin's solution is a nice one and scales better than mine. However mine is a little bit easier on the fingers as it does not require the --skip-bundle option.

ruby
  def test
    @generator.gem 'rspec-rails', group: [:test, :development]
    at_exit do
      generate 'rspec:install'
    end
  end
Avatar

You can work around the issue with running bundle install multiple times with the --skip-bundle-option for the rails-command and calling bundle_command('install') from the leftovers-method:

app_builder.rb
class AppBuilder < Rails::AppBuilder
  def test
    @generator.gem 'rspec-rails', group: [:test, :development]
  end

  def leftovers
    bundle_command('install')
    generate 'rspec:install'
  end
end
terminal
rails new foo --skip-bundle -b app_builder.rb

It isn't the most elegant solution, but it saves a little time by calling bundler only once. It would be nice if you could modify the options-variable in the AppBase-class

Avatar

Ryan, you could provide the source code before and after the screencast... that would be amazing...

  1. the first source code, so we can follow you

  2. the second source code, so we can check the app actually working

:)

Avatar

i am using :window=>1 for my pagination. by this i'm able to sho only 2 pages i.e. 1,2 when user 1st time see, but i want to show here 1,2,3..... using window =>1.
How can i show 3 page when 1st time user see the page.

Avatar

Late response but it can be usefull for someone else.

There's no "LIKE" command for MongoDB, you can perform a search with a regex.

ruby
Project.where(name: /yard/i)

But as always there's already some gem that do the work for you :
For Keyword search : https://github.com/mauriciozaffari/mongoid_search
For FullText search : https://github.com/artsy/mongoid_fulltext

Avatar

For Mongoid

ruby
def self.current(except = nil)
  now = Time.current
  result = where(:starts_at.lte => now, :ends_at.gte => now)
  result = result.where(:id.nin => except) if except.present?
  result
end

lte : less than or equal
gte : greater than or equal
nin : not in

I really love the Mongoid syntax.

Avatar

Last few seconds of this talk appears to be cut off as well.

Avatar

Good information, but most of these steps are almost hard coded into my brain already. However, I do see where it can be useful for creating templates for certain configurations with twitter-bootstrap, cancan, sorcery/devise, user model, etc. with the most commonly used gems.

Though, usually it only takes a few minutes to do this stuff now.

Didn't know about the appscrolls. Does seem useful.

Avatar

Did you get this fixed? I'm having the same issue!

Avatar

When I install the Rubber gem, I only have access to the config command. I.e. I can't call Vulcanize. See my post on Stack Overflow for a print screen: http://stackoverflow.com/questions/14522051/rails-rubber-gem-no-such-sub-command-vulcanize/14536914#14536914

Does anybody know how to fix this? It sounds like it could fix @kmamit's problem above too.

Avatar

I think I resolved this by removing the deferred option in config/nginx.conf

server {
listen 80 default deferred;
}

Avatar

Did you find a solution to this? I'm having the same issue.

Avatar

Ryan uses the rails executable in the episode. It is important to mention that depending on your setup, that probably will call your 3.2.X install of rails. While Rails is in beta and there is no prerelease gem, you need to use bin/rails:

$ rails -v
Rails 3.2.11

$ bin/rails -v
Rails 4.0.0.beta

This bit me when I found that rails s did not start the server.

Avatar

Years later, this is still very helpful, great ep !

Avatar

I am trying to use RABL with a model structure using inheritance. I fail setting up a matching template.
Maybe you can take a look here.

Avatar

The link you posted got me partway there, but I rewrote the cvim file so that it works for both better_error and RailsPanel.

https://gist.github.com/4699980

Avatar

11 months later...the answer to this question is capistrano (as well as some other applications) expects to find a database owned by a user of the same name 'deployer' (read that in the postgres documentation). I'm wondering if you happen to be developing in windows so won't be using agent-forwarding, and I suspect this is the root cause of permissions errors.

A perhaps related problem...I'm getting a permission-denied error when the capistrano task deploy:start/restart attempts to run. Anyone know why? Anyone still read these 11 months later?

Avatar

Solved it...

Had to set jQuery to noConflict(), convert the script to jQuery and add it to a javascript file. Dunno why this happens though...

Avatar

anyone have tips on creating a dynamic query with Squeel. for example if i have an array of conditions [[column,operator,value],[column,operator,value]]

How would i iterate over those and dynamically build a query in squeel?

in ultra basic format

ruby
def self.with_conditions(conditions)
    where do
        conditions.each do |column,operator,value|
            (column operator value)
        end
    end
end