RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Here is the solution for those who may want it:

ruby
def self.tagged_with(name) #name is an array of 1 or more tag names
    all(:conditions => {:tags => {:name => name}},
      :joins      => :taggings,
      :joins      => :tags,
      :group      => 'articles.id',
      :having     => ['COUNT(*) >= ?', name.length]
    )
end
Avatar

Adding:
*= require bootstrap_and_overrides
to the applications.css solved.
Stackoverlow

Avatar

thank you : it worked here too :

def self.tokens(query)
filter_query = query.gsub(/[^a-zA-Z&\s]/, '')
filter_query = filter_query.strip
topics = where("LOWER(name) like ?", "%# {filter_query.downcase}%")
topics << {id: "<<<#{query}>>>", name: "New: \"##{filter_query}\""}
end

Avatar

After some exploration, actually you can still use the url_helpers such as edit_product_path etc by just including the rails url_helper in your class e.g.

ruby
class YourDatatable
    include Rails.application.routes.url_helpers
.
.
def data
.
link_to("edit", edit_product_path(product)
end
end
Avatar

Hi Ryan! Thanks for the challenge! It sounds really cool :) +1

Avatar

Can anybody point me for which environment does this episode work for? Is it written somewhere?

Avatar

I'm using Rails 4.0.0.rc1 and ruby 1.9.3p392.

It seems this episode won't work for me.
I'm having errors:

LoadError in Products#index

Showing /home/askar/work/rails_studio/store/app/views/layouts/application.html.erb where line #5 raised:

cannot load such file -- less
(in /home/askar/work/rails_studio/store/app/assets/stylesheets/bootstrap_and_overrides.css.less)
Extracted source (around line #5):

2
3
4 Store
5 <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6 <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7 <%= csrf_meta_tags %>
8

Rails.root: /home/askar/work/rails_studio/store

Application Trace | Framework Trace | Full Trace
app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb__3286420188203285275_35132680'

Avatar

Question! I am trying to eliminate the use of regular model IDs to access my pages. IE, right now, you can still go events/1, as opposed to just the custom (for me it's a hash) such as events/12k5s
I want to eliminate any possible guessing of other pages. How would I eliminate the old option?

Avatar

I wonder why Ryan's code doesn't include them in Gemfile...

Avatar

Mauricio,

I have an existing rails app that uses devise and I want to connect to it using backbone.js from another app.

Were you able to obtain any feedback as to the best way to go about this?

Kind regards

Andrew

Avatar

Hi Ryan!

Any plans to revise this with Rails 4 ?

Avatar

Hey anso,

Use the normal link_to :

ruby
link_to('Edit', :controller => 'products', :action => 'edit', :id => product)
Avatar

Ever figure this one out TopperH? I've been struggling with the same problem.

Avatar

I don't know how to get the link of an image I upload in a page, with a data-mercury="image".

For example, i have this:
<div> id="page_content" data_mercury="full"></div>

I get it in the controller like this:
params[:content][:page_title][:value]

How to do with ?:
<img id="article_image" data-mercury="image">

Thank you

Avatar

May be you should convert ActionDispath to Array or String.

Avatar

Search and tags will give you more suggestion.

Avatar

Does anyone know why has rails not adopted EventMachine? When i google around, i am unable to find any good concrete examples of usage of EM with Rails3.2. Would we have any out of the box compatibility from Rails4 for EM?

Avatar

My nginx.conf is OK and nginx seems to start but my https URL in Google Chrome gives me an this error:

SSL connection error
Unable to make a secure connection to the server. This may be a problem with the server, or it may be requiring a client authentication certificate that you don't have.
Error 107 (net::ERR_SSL_PROTOCOL_ERROR): SSL protocol error.

When I do a lsof -i:443 I see this:

COMMAND     PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
apsd         71   root    9u  IPv4 0xcb28d77b1a14342f      0t0  TCP 10.0.1.32:54135->17.172.232.214:https (ESTABLISHED)
nginx     11899   root    6u  IPv4 0xcb28d77b188cb1a7      0t0  TCP *:https (LISTEN)
nginx     11900 nobody    6u  IPv4 0xcb28d77b188cb1a7      0t0  TCP *:https (LISTEN)
Google    68100    Aen   43u  IPv4 0xcb28d77b16b478df      0t0  TCP 10.0.1.32:54191->ni-in-f125.1e100.net:https (ESTABLISHED)
Google    71302    Aen  155u  IPv4 0xcb28d77b1b2201a7      0t0  TCP 10.0.1.32:55087->sin04s01-in-f30.1e100.net:https (ESTABLISHED)
Google    71302    Aen  173u  IPv4 0xcb28d77b1b236017      0t0  TCP 10.0.1.32:55065->ni-in-f147.1e100.net:https (ESTABLISHED)
Google    71302    Aen  185u  IPv4 0xcb28d77b175de42f      0t0  TCP 10.0.1.32:55123->github.com:https (ESTABLISHED)
Google    71302    Aen  194u  IPv4 0xcb28d77b1b23b74f      0t0  TCP 10.0.1.32:55127->ec2-204-236-241-186.compute-1.amazonaws.com:https (ESTABLISHED)
Google    71302    Aen  217u  IPv4 0xcb28d77b1b1d88df      0t0  TCP 10.0.1.32:54361->sin01s05-in-f2.1e100.net:https (ESTABLISHED)
Google    71302    Aen  260u  IPv4 0xcb28d77b1b245017      0t0  TCP 10.0.1.32:55078->a23-15-10-57.deploy.akamaitechnologies.com:https (ESTABLISHED)
Dropbox   95185    Aen   18u  IPv4 0xcb28d77b1b240017      0t0  TCP 10.0.1.32:55130->108.160.160.177:https (CLOSE_WAIT)

Do I have other crap listening on 443?

Avatar

I have an issue with this when im deploying my app to heroku, the vestal_versions gem works great locally but not on heroku. It seems that the versions are not working and my records are not being updated when I edit a post.

Anyone know whats going on?

Thanks, Vincent.

Avatar

Same here. I'll post if I figure something out.

Avatar

I know this is an old episode but I wanted to clarify the path used for the pid. In the config/mailit.god file in the God.watch block you have this path for the pid:

ruby
  w.pid_file = "#{RAILS_ROOT}/log/workling.pid"

Is not the convention for pids to be put in the tmp folder? Like this:

ruby
  w.pid_file = "#{RAILS_ROOT}/tmp/workling.pid"

Hope you can reply...

Avatar

How do I allow a non technical user to change a products order form?

like add products, and change prices?

Avatar

If you are using Paginate/kaminari with Sunspot use this code with in the index in the Controller.

@search = Sunspot.search(Snippet) do
     fulltext params[:search]
  end
    @snippets = Snippet.where(id: @search.results.map(&:id)).page(params[:page]).per(5)
Avatar

Never mind, must have been too little sleep. Using a class_name solves it.

Avatar

I have a problem with nested forms and a polymorphic model. Using comments as an example, how does one add multiple has_many :through for the comments needed.
Example:

has_many :comments, :through => :images
has_many :comments, :through => :articles

The second has_many overwrites the first one and :through only allows one reference.

Any tips appreciated.

Avatar

Hi Ryan, thx for this great episode !

One question:
How could i make paperclip to work with your tutorial ?

Thx !

Avatar

Do you have this in your class? table-striped

Avatar

I suggest you look at your Rails console to see if there is a message showing an error. It's likely a data read error.

Avatar

The following puts each record into a dataTable cell instead of a row:

ruby
def data
    User.current.employee.workgroups.map do |workgroup|
      workgroup.workorders.wonotclosed.map do |workorder|
        [
        link_to(workorder.wonum, workorder),
        h(workorder.parent.try(:wonum)),
        h(workorder.description),
        h(workorder.worequest.try(:subject)),
        h(workorder.client.try(:client_name)),
        h(workorder.woasset.try(:assetnum)),
        h(workorder.type.try(:typecode)),
        h(workorder.priority),
        h(workorder.super.try(:employee_full_name)),
        h(workorder.workgroup.try(:group_name)),
        h(workorder.employee.try(:employee_full_name)),
        h(workorder.wostatus.try(:statuscode))
        ]
      end
    end
  end

Any way to fix?

Avatar

My next polymorphic task is to add this exact function to a small app I have. I thought I was starting easier to just have an address field shared across models but there's not much out there to help with that, especially for Rails 4. I have put together how I got an address polymorphic association working in my app at my blog if anyone needs that help.

http://hamcois.com/articles/6

Avatar

Another option is X-editable. It offers similar functionality, but uses Bootstrap/JqueryUI/Jquery elements. Really works fantastic in my Bootstrap UI.

Avatar

ActiveAdmin helped us get up and running quickly, but once we had use cases outside of very simple CRUD, it became nightmarish.

  • You can't inherit pages e.g. for an STI parent class
  • There's no obvious base application controller
  • Having everything in the app/admin/foos.rb quickly makes the file huge. Would love some separation of concerns
  • Its conventions are so different from rails
  • It's pretty hard to customize. It requires a lot of reading source code and hacking
  • It overwrites any existing login/logout routes you have

Do read this: http://staal.io/blog/2013/02/26/mastering-activeadmin/

I did try rails_admin first because it seemed more customizable but it didn't have all the features I wanted

Avatar

Add these gems to your Gemfile:

gem "therubyracer"
gem "less-rails"

Avatar
ruby
class Author
  ...
  key :name # was removed in Mongoid3
end

You're supposed to override the _id field:

ruby
class Author
  ...
  field :_id, type: String, default: ->{ name }
end

Docs: http://mongoid.org/en/mongoid/docs/upgrading.html

Avatar

Still no chance?
No one has the same problem?

Avatar

correction with open_spreadsheet method

def self.open_spreadsheet(file)
case File.extname(file.original_filename)
when ".csv" then Roo::Csv.new(file.path, nil, :ignore)
when ".xls" then Roo::Excel.new(file.path, nil, :ignore)
when ".xlsx" then Roo::Excelx.new(file.path, nil, :ignore)
else raise "Unknown file type: #{file.original_filename}"
end
end

Avatar

Did you solve this issue? I've having the same problem.

Avatar

Were you able to solve this issue? I'm running into the same problem.

Avatar

I know this is 6 months old, but:

You'll want to handle this in your routes.rb file.

Something like:

get ":id" => 'users#show', as: :name_your_path

Avatar

you are populating the statistics_chart div with data from data-statistics attribute of that div, look in inspect element and you'll see the html5 attribute called data-statistics with an array of hashes inside.

Avatar

I followed this RailsCast tutorial, then later discovered csv_builder. Like you said, the template/view approach is much better! I think this RailsCast should be revised to use csv_builder rather than putting CSV view logic in the Controller as it recommends!

Note new repo URL: https://github.com/gtd/csv_builder

Avatar

Is there any way to update attributes for miltiple objects with nested form?

I used this code in controller:

ruby
@works = Work.find(params[:work_ids])
@works.reject! do |work|
   work.update_attributes!(params[:work])
end

if @works.empty?
   redirect_to contracts_path
else
   render :edit_multiple
end

It is similar to example

But instead of updating the records, Ruby creates new ones and nullifies "work_id" in existing records.

mysql> SELECT * FROM work_comments;
+----+---------+-------------+
| id | work_id | content |
+----+---------+-------------+
| 1 | NULL | comment |
| 2 | NULL | comment |
| 3 | NULL | comment |
| 4 | 1 | new comment |
| 5 | 2 | new comment |
| 6 | 3 | new comment |
+----+---------+-------------+
6 rows in set (0.00 sec)

What could cause this problem? Thank you!

Avatar

2013blahblahblahfoo000 is basically a timestamp with milliseconds so even repeating the action immediately after the first action the value/cache_key cannot be the same.

Avatar

I have created a list of gems for Development Machine in Ruby on Rails to make to your development life much more easier/productive

Avatar

Hi Ryan,

I was hoping you could weigh in here, I'd like to create a rubber capistrano recipe to deploy my rails app using nginx and phusion passenger enterprise edition. The main problem I have is changing the rubber tasks to install the passenger gem from a local file instead of from the open-source repo. If you could please provide some guidance I would be very appreciative.