rails
Ruby on Rails is a #web #framework for #ruby
Ruby on Rails is a #web #framework for #ruby
In Gemfile:
gem 'capybara'
gem 'poltergeist'
The Problem:
$ rake spec
...
Failures:
1) An Example
Failure/Error: fill_in 'admin_email', with: admin.email
NotImplementedError:
NotImplementedError
...
Given an image accessor field cover_image for an Image model, u can use a cover_image_url accessor for assigning directly from a url (simply using a string). To make it does what u want just save the corresponding object.
example:
img = Image.new
img.cover_image_url = 'http://img.youtube.com/vi/wh-XcmRQFhI/0.jpg'
img.save
BTW: what is your favourite image uploader in rails?
I have a lot of code in my project like the following:
class Animal < ActiveRecord::Base
state_machine do
around_transition :on => :sure do |animal, _, block|
animal.transaction do
block.call
animal.do_things_on_sure
end
end
end
end
around_transition looks ugly and repeats for many classes.. Lets refactor it. More under the cut
Here is a typical bad case in #rails:
class Band < ActiveRecord::Base
has_many :members, dependent: :destroy
end
class Member < ActiveRecord::Base
belongs_to :band
after_destroy do
Resque.enqueue BadNewsWorker, id
raise('surprise') # real world :(
end
end
band = Band.create
band.members.create
band.destroy # => RuntimeError
Band.count => 1
I had a custom collection that I wanted to render using Rails' render :partial, :collection syntax. Digging into the partial_renderer.rb code I found that my collection had to respond to the following methods:
If you skip any of those, you'll either get an error or your partial simply won't render, depending. More under the cut
For example by namespace. Just create new folder for #routes config/routes/ and tell #rails to include it:
config.paths["config/routes"] += Dir[Rails.root.join("config/routes/*.rb")]
Then you can create files like this:
# config/routes/admin.rb
Pilot::Application.routes.draw do
namespace :admin do
resources :posts
end
end
Sometimes there are some external validations that are brought in our #active_record models by some gems. Usually these validations are useful, but sometimes we don't want such validations, if we want redefine some gem's behavior.
So, I show you how to cancel validation in ActiveRecord model, for example Devise's validation of email uniqueness. More under the cut
You can refactor template's code with capture method:
/ before:
li
= mail_to 'info@gistflow.com', '<i class="icon-envelope"></i><span>Email</span>'.html_safe, class: 'secondary', encode: 'hex'
/ after:
li
- name = capture do
i class="icon-envelope"
span Email
= mail_to 'info@gistflow.com', name, class: 'secondary', encode: 'hex'
Very useful :)
This snippet will provide a :nudge option for belongs_to associations, which behaves like the belongs_to :touch => true did in Rails 2.x. It will use save! instead of a sneaky update_all call, so all of your callbacks and validations will be invoked. More under the cut
If you need work with serialize fields like a object attributes More under the cut
A nice little hack to avoid passing the argument to Enumerable#map. Specially handy when you manipulate ActiveRecord collections in the #rails #console.
class Array
def map(&block)
block.arity < 0 ?
map { |obj| obj.instance_eval &block } :
collect(&block)
end
end
now you can do : More under the cut
Sometimes you want to automatically redirect users based on their user-agent string to a different subdomain. You also probably want to give them an option to override the default and visit the alternative site, and it would be even better to remember their decision in a cookie.
Here is how to do it with rack middleware. More under the cut
I am trying to create a calendar table to display the daily availability of every staff member of a company. What I think I need is a header with the days of the month and then a row (with as many cells as the number of the days) for each staff member. The different background color of each cell will represent a different availability status and the staff members change so I want all this to be dynamically generated. Click for More!
A super handy Hash class extension which allows you to manipulate it's keys based on a specific logic. It also adds the option to recursively affect all Hashes found in the structure. More under the cut
On http://mailtrap.io you can sign up and register a mailbox.
Then you get an email config's setup for development env.
All emails will be sent to mailtrap and it won't be delivered to real users. It is very important when you have a real db for development.
This service is absolutely free.
Have you ever face a situation where unique validations in your #rails app didn't work? Under the cut a little story about this situation in project that I maintain and solution how broken records (duplicates) were fixed. More under the cut
State Machine comes to the rescue when models in your application need statuses and events.
There is a #state_machine gem for #ruby.
I recommend you to spend some time studying basics before you start using it with #rails. More under the cut
Is it obvious and expected behavior:?
If we apply where(:some_table => {}) (empty Hash) for joined table, AR append AND (1 = 2) to SQL so empty results
# and even
User.where(:blabla => {})
# => SELECT `users`.* FROM `users` WHERE (1 = 2)
Let me dilute all our programming posts and questions with article mostly related to startups. On example of simple application we will depict some of the project's KPIs with Google Charts and some tricky ActiveRecord queries.
Could not resist to share it :)
Just see video: http://vimeo.com/32424001
Site: http://mattsears.com/articles/2011/11/16/nyan-cat-rspec-formatter Screenshot under cut
I used to have certain problems with testing #email views while developing #rails app.
And I found a nifty solution distributed by 37signals called mail_view gem. More under the cut
Besides link_to, form_for with remote: true you can get the same behaviour with any inputs.
Let's take a look at the source code of jquery-rails: More under the cut