Paperclip::Errors::MissingRequiredValidatorError with Rails 4

Starting with Paperclip version 4.0, all attachments are required to include a content_type validation, a file_name validation, or to explicitly state that they’re not going to have either. Paperclip raises Paperclip::Errors::MissingRequiredValidatorError error if you do not do any of this. In your case, you can add any of the following line to your Post model, … Read more

Auto-loading lib files in Rails 4

I think this may solve your problem: in config/application.rb: config.autoload_paths << Rails.root.join(‘lib’) and keep the right naming convention in lib. in lib/foo.rb: class Foo end in lib/foo/bar.rb: class Foo::Bar end if you really wanna do some monkey patches in file like lib/extensions.rb, you may manually require it: in config/initializers/require.rb: require “#{Rails.root}/lib/extensions” P.S. Rails 3 Autoload … Read more

Add querystring parameters to link_to

The API docs on link_to show some examples of adding querystrings to both named and oldstyle routes. Is this what you want? link_to can also produce links with anchors or query strings: link_to “Comment wall”, profile_path(@profile, :anchor => “wall”) #=> <a href=”http://stackoverflow.com/profiles/1#wall”>Comment wall</a> link_to “Ruby on Rails search”, :controller => “searches”, :query => “ruby on … Read more

Ruby on Rails: Where to define global constants?

If your model is really “responsible” for the constants you should stick them there. You can create class methods to access them without creating a new object instance: class Card < ActiveRecord::Base def self.colours [‘white’, ‘blue’] end end # accessible like this Card.colours Alternatively, you can create class variables and an accessor. This is however … Read more

Rails – How to use a Helper Inside a Controller

You can use helpers.<helper> in Rails 5+ (or ActionController::Base.helpers.<helper>) view_context.<helper> (Rails 4 & 3) (WARNING: this instantiates a new view instance per call) @template.<helper> (Rails 2) include helper in a singleton class and then singleton.helper include the helper in the controller (WARNING: will make all helper methods into controller actions)