RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Ryan, you can use <<-RUBY instead to get highlighted heredoc in Textmate.

Avatar

Since I've started using Draper, I've generally been pretty happy with it. The one thing that keeps biting me is object equality (decorated_object != object) and in general code that expects to be dealing with an actual instance of the model class and not an instance of the decorator behaves differently or incorrectly when your object isn't actually an instance of your model class.

If @user is a UserDecorator object and current_user is a User object, for example, then even a simple test like this:

ruby
if @user == current_user
  ...
end

doesn't work as expected.

Have also had some occasional odd behavior when working with CanCan. (To get error messages to work as expected, for example I had to add a section for user_decorator: in addition to user: in my locale file because it looks up messages based on the object's class.)

I wish Draper worked by mixing in a module instead of creating a an entirely new class. Then a decorated User object, for example, would still be a User and object equality would work as expected.

But I guess Draper was only intended to be used in the view layer, so my mistake was probably that I am decorating objects in the controller as soon as I fetch them from the database. Maybe I will try to only create and use the decorated objects in the view and see if that works any better...

Avatar

Thnx your solution works for my Dutch website where I rename my resources to Dutch words like so:

resources :users, :path => "gebruikers" do
resources :comments
end

Avatar

A Newbie question. If a user typed a misspelled or mistype word e.g graphc rather than graphic, will it still give some result. What's the best way to handle that kind of situation as i can't find any plugin for "did you mean? graphic" functionality.

Avatar

Hi there,

first of all thanks to Ryan for a great RailsCast again and thanks to Sam for this wonderful gem.

No problem with installing the gem as expected. But I also do not see any SQL query times while profiling in development mode. Here's my setup:

ruby-1.9.3-p194 [ x86_64 ]
Using rails (3.2.8)
Using rack (1.4.1)
Using sqlite3 (1.3.6) for development
Using pg (0.14.1) for production (no MySQL gem)
Using newrelic_rpm (3.4.2)
Using thin (1.4.1)

Any progress in examining this issue? Do you want me to post this question / issue on http://community.miniprofiler.com ?

Thanks a lot

Andy

Avatar

I would imagine Rails 4 handles it a lot better once it's released.
The great thing about this is you get the benefits of schemaless data with your schema'd data... and you get to keep using AR.

Avatar

Seems like you missed 'provider' in your mock.

Avatar

Yes! There is a way to compile Ruby to Java...try to type 'jrubyc' in the shell...it simply takes Ruby files and converto to java .class
If you want a compiled version of your Rails app try:

$ warble compiled war

Avatar

Works great for me, but the cucumber test fails. I get “Password can't be blank” messages. I guess it is related with using “:without_protection => true” while creating new User.

step_definitions/user_steps.rb
def set_facebook_omniauth
 credentials = {:provider => :facebook, :uuid => '65', :info => {:email => 'machete@yourgarganta.mx', :name => 'Denny Trejo'}}
 OmniAuth.config.test_mode = true
 OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({'uid' => credentials[:uuid], 'info' => {'email' => credentials[:info][:email], 'name' => credentials[:info][:name]}})
end

When /^I login with Facebook$/ do
 set_facebook_omniauth
 visit root_path
 click_link 'Login with Facebook'
end

Then /^I see a successful sign in message$/ do
 page.should have_content "Signed in successfully."
end

As I’m using mongoid I’ve changed a bit “from_omniauth” method.

models/user.rb
 def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).find_or_create_by do |user|
     user.provider = auth.provider
     user.uid = auth.uid
     user.name = auth.info.name
     user.email = auth.info.email
    end
 end
Avatar

I've successed to deploy my private_sub app to heroku today
I'dont understand about rack application yet
anyway, for Faye server in Gemfile I added 3gems , private_pub, thin, and foreman
and make file 'Procfile' in rails root, like this

ruby
 web: bundle exec thin -p $PORT -e $RACK_ENV -R private_pub.ru start

then git push heroku master as usual, and done
When you access faye server address, if you show message 'Sure you're not looking for /faye ?' ,
faye server is good working.

Avatar

Can anyone describe how FnordMetric compares/contrasts to graphite-statsd??

Avatar

Should also have "set :normalize_asset_timestamps, false" in deploy.rb

Avatar

They're different creatures. Squeel is a DSL that works with ActiveRecord -- Sequel is an entirely different ORM.

Avatar

If you're using twitter-bootstrap-rails gem sumply update your gemfile by running bundle update run the install task for the gem once again and then if you're using the asset pipeline you can run rake assets:precompile. If that doesn't work go to the official gem repository they have a link to their mailing list where you can ask them directly.

Avatar

I was hoping so, but all I can see is Reply. It appears you can only Delete within a certain timeframe.

Avatar

Are you saying you set :attr_accessible nil and it won't let you assign any attributes? If that's the case, just manually set them when necessary. New/create/build take a block form so you can do this somewhat nicely:

Something.create(params[:something]) do |thing|
  thing.attr = params[:something][:attr] 
end

while I realize that seems silly, it's better than the alternative :)

Avatar

Are you saying you set :attr_accessible nil and it won't let you assign any attributes? If that's the case, just manually set them when necessary. New/create/build take a block form so you can do this somewhat nicely:

Something.create(params[:something]) do |thing|
  thing.attr = params[:something][:attr] 
end

while I realize that seems silly, it's better than the alternative :)

Avatar

That seems pretty well organized. But then, if you have a dozen helper files and access them from a dozen different views, then if you are looking in the view and you see a call to a method, you really have no idea where it is defined, and most people use their IDE to find it. And what happens if you use the same name for a method in each of 2 different helper modules?

And these helper methods are for views, anyway, not for controllers, so I don't think it is very nice to have to include them in the controller. I think it would be much better if we were including them in the views, or perhaps just calling them with their module name and method name.

Is anyone else underwhelmed by the way the helpers work?

Avatar

Hi
I tried this gem today
if you follow this video, your faye server :9292 is in production env, but your rails server :3000 is in development env.
so when you access from remote anything but localhost, you describe your faye server address in "config/private_pub.yml".
like this....
development:
server: "http://192.168.1.20:9292/faye"
secret_token: "secret"

i hope it will be help

Avatar

FYI, I just wrote about my experience with Compass and CSS Sprites. Check it out here.

Avatar

Hi there,

can someone offer this tutorial as a "non-square-thumbnail" example?

I ran into render issues when I try to change the parameters to a "rectangle" in the js.coffee file

Avatar

Is it possible to use this to make a review system? For example: Have a five star rating system with comments/text belonging to each vote?

Avatar

Cool but doesn't work if you mix new and existing records.

Avatar

I am going to integrate the event tracking into my system. Will this lead to some performance issue when real time access increased sharply?

Avatar

For local development you can use the domain http://lvh.me. Some kind soul purchased it and pointed it to localhost.

Avatar

Thanks for this screencast, during my migration to chef-solo I started working on a solution which makes it super easy to install a more or less standard rails stack so that you can start deploying via capistrano:

https://github.com/dei79/solify

Avatar

Really a great technique. Rather than using a querystring param, I choose to use the URL extension. It just felt better than a querystring since Rails already responds to that to determine the correct MIME format:

ruby
def mobile_device?
  session.has_key?(:mobile) ? session[:mobile] : request.user_agent =~ /Mobile|webOS/
end
helper_method :mobile_device?

def prepare_for_mobile
  if request.format == 'text/html'
    case File.extname(URI.parse(request.fullpath).path)
    when '.html'
      session[:mobile] = false
    when '.mobile'
      session[:mobile] = true
    end
    request.format = :mobile if mobile_device?
  end
end
Avatar

I see that fnordmetric is namespacing redis. Can this be easily integrated with resque using the same redis instance?

Avatar

Does Squeel support hstore?

Is there a good way to implement this functionality?

Avatar

Does anyone know whether and if so how it's possible to use fnordmetric on heroku?

Avatar

There's also Image Sorcery, which allows you to directly use the ImageMagick command line tools.

Avatar

You don't have to. If you're not using strong_parameters, just use :without_protection => true. See the last example at http://api.rubyonrails.org/classes/ActiveRecord/AttributeAssignment.html#method-i-assign_attributes

Avatar

If you're using Pow.cx, you could define these in .powenv file. If you're using foreman, you could define it in .env file in your app directory

Avatar

This is happening to me. Very frustrating.

Avatar

Does this make performance faster than erb or its all about how to make your own template? nice screen cast.

Avatar

There is a little information about doing so here: https://github.com/paulasmuth/fnordmetric/issues/60

I've just now asked for an example / gist

Avatar

Interesting screencast. Could it be a mountable engine? Is possible to protect it with authentication?

Avatar

I'm using draper and in my controller's index action, i have following code

PostsController
@posts = PostDecorator.approved.order('created_at DESC').includes(:user, :category).page(params[:page]).per(5)

and in my view

index.html.erb
<ul>
    <%= render @posts %>
</ul>
<%= paginate @posts %>

and I have a partial, _post.html.erb
I want to access methods defined in PostDecorator and UserDecorator in my partial
like post.post_title

What should i need to do to pass @posts variable into my _post partial and access decorator methods

I tried following but failed
<%= render @posts, locals: { post: @posts} %>
and
<%= render @posts, object: @posts %>

Please help me
Thanks :)

Avatar

ah I was looking for this for half a day! :) Thank you!!

Avatar

I think it's not convenient to debug in action of rails, because there's no list, step, next method, while we can't call an action directly.

Avatar

You could just:

ruby
request.user_agent =~ /android|blackberry|iphone|ipad|ipod|iemobile|mobile|webos/i

Rather than going through each individually and you can ignore case.

Avatar

Nice. I recently did something like this, but I used the friendly_id gem instead for the friendly url stuff. Nice info nonetheless.

Avatar

Does anyone know how to change default jvm options (heap size, stack size) that trinidad_init_services uses to generate init script ?

Avatar

Possible that this is due to my out of date (but unupdateable) rubygems!

Avatar

Hey Ryan, an important heads-up!

You actually need to do
require "RMagick"

and not
require "rmagick"

This works on Mac OS because of the commonly case-insensitive filesystem, but once you deploy on a linux server, the all small-caps won't function correctly. Took me a while to figure out this discrepancy.

Avatar

Does creating a new category belong in the model? - It seems a bit weird to have it in the model rather than the controller, or is it just me?