Rails: Open link in new tab (with ‘link_to’)

The target: :_blank parameter should be a parameter of link_to, whereas you put it in image_tag parameters. Modify your code like this: <%= link_to image_tag(“facebook.png”, class: :facebook_icon, alt: “Facebook”), “http://www.facebook.com/mypage”, target: :_blank %> Or with a block: <%= link_to “http://www.facebook.com/mypage”, target: :_blank do %> <%= image_tag(“facebook.png”, class: :facebook_icon, alt: “Facebook”) %> <% end %>

Can someone explain collection_select to me in clear, simple terms?

collection_select( :post, # field namespace :author_id, # field name # result of these two params will be: <select name=”post[author_id]”>… # then you should specify some collection or array of rows. # It can be Author.where(..).order(..) or something like that. # In your example it is: Author.all, # then you should specify methods for generating options … Read more

How to prevent browser page caching in Rails

I finally figured this out – http://blog.serendeputy.com/posts/how-to-prevent-browsers-from-caching-a-page-in-rails/ in application_controller.rb. After Ruby on Rails 5: class ApplicationController < ActionController::Base before_action :set_cache_headers private def set_cache_headers response.headers[“Cache-Control”] = “no-cache, no-store” response.headers[“Pragma”] = “no-cache” response.headers[“Expires”] = “Mon, 01 Jan 1990 00:00:00 GMT” end end Ruby on Rails 4 and older versions: class ApplicationController < ActionController::Base before_filter :set_cache_headers private def … Read more

Bundler: Command not found

My problem was that I did: sudo gem install bundler So I had installed as root rather than as myself. So I uninstalled as root, then installed as myself: sudo gem uninstall bundler gem install bundler rbenv rehash (last command for if you are using rbenv) And it worked. The “correct” path was in .bashrc … Read more

Labels for radio buttons in rails form

Passing the :value option to f.label will ensure the label tag’s for attribute is the same as the id of the corresponding radio_button <%= form_for(@message) do |f| %> <%= f.radio_button :contactmethod, ’email’ %> <%= f.label :contactmethod, ‘Email’, :value => ’email’ %> <%= f.radio_button :contactmethod, ‘sms’ %> <%= f.label :contactmethod, ‘SMS’, :value => ‘sms’ %> <% … Read more

Rails Observer Alternatives for 4.0

Take a look at Concerns Create a folder in your models directory called concerns. Add a module there: module MyConcernModule extend ActiveSupport::Concern included do after_save :do_something end def do_something … end end Next, include that in the models you wish to run the after_save in: class MyModel < ActiveRecord::Base include MyConcernModule end Depending on what … Read more

A copy of xxx has been removed from the module tree but is still active

Tenant is sort of a red herring – the error would occur if you referenced any bit of app that needs to be loaded by rails’ const_missing trick. The problem is that you are taking something reloadable (your module) and then including it in something not reloadable (ActiveRecord::Base or, in your earlier example ActionMailer::Base). At … Read more