Rails 3.1 asset pipeline: how to load controller-specific scripts?

To load only the necessary name_of_the_js_file.js file: remove the //=require_tree from application.js keep your js file (that you want to load when a specific page is loaded) in the asset pipeline add a helper in application_helper.rb def javascript(*files) content_for(:head) { javascript_include_tag(*files) } end yield into your layout: <%= yield(:head) %> add this in your view … Read more

Rails: How to add link_to with target blank

Why would you want to override link_to? It’s already defined in Rails, just use it like this : link_to “GOOGLE”, “http://www.google.com”, target: “_blank”, class: “btn btn-large btn-primary” Edit: OK, understood. I’d advise against overriding such a common method so create another one : def link_to_blank(body, url_options = {}, html_options = {}) link_to(body, url_options, html_options.merge(target: “_blank”)) … Read more

Add a new asset path in Rails 3.1

Andrew, app/assets/fonts is actually already in your asset load path, along with images. So you can just point to the asset in the same way: <%= asset_path(‘/Ubuntu/Ubuntu-R-webfont.eot’) %>[1] or how ever you are referencing your images. It took me a while to wrap my head around this as well. I still don’t know what happens … Read more

When to use self in Model?

When you’re doing an action on the instance that’s calling the method, you use self. With this code class SocialData < ActiveRecord::Base def set_active_flag(val) active_flag = val save! end end You are defining a brand new scoped local variable called active_flag, setting it to the passed in value, it’s not associated with anything, so it’s … Read more

rails – “WARNING: Can’t verify CSRF token authenticity” for json devise requests

EDIT: In Rails 4 I now use what @genkilabs suggests in the comment below: protect_from_forgery with: :null_session, if: Proc.new { |c| c.request.format == ‘application/json’ } Which, instead of completely turning off the built in security, kills off any session that might exist when something hits the server without the CSRF token. skip_before_filter :verify_authenticity_token, :if => … Read more

Subqueries in activerecord

Rails now does this by default 🙂 Message.where(user_id: Profile.select(“user_id”).where(gender: ‘m’)) will produce the following SQL SELECT “messages”.* FROM “messages” WHERE “messages”.”user_id” IN (SELECT user_id FROM “profiles” WHERE “profiles”.”gender” = ‘m’) (the version number that “now” refers to is most likely 3.2)

Changing a column type to longer strings in rails

You should use text with Rails if you want a string with no length limit. A migration like this: def up change_column :your_table, :your_column, :text end def down # This might cause trouble if you have strings longer # than 255 characters. change_column :your_table, :your_column, :string end should sort things out. You might want :null … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)