ruby-on-rails-3.1

rails 3.1 asset pipeline css caching in development

若如初见. 提交于 2019-11-28 17:25:37
问题 I'm a bit confused as it seems like the application.css is including itself twice, once when it lists the resources from the manifest and then a cache of that. So when I delete an individual file it still seems to stay alive inside the application.css file. application.css (source) /* *= require twitter/bootstrap *= require_self *= require_tree ./common *= require_tree ./helpers */ Which works as expected and outputs in dev mode all the relevant individual files development.rb # Do not

Find number of months between two Dates in Ruby on Rails

人走茶凉 提交于 2019-11-28 17:07:58
I have two Ruby on Rails DateTime objects. How to find the number of months between them? (Keeping in mind they might belong to different years) Massimiliano Peluso (date2.year * 12 + date2.month) - (date1.year * 12 + date1.month) more info at http://www.ruby-forum.com/topic/72120 A more accurate answer would consider days in the distance. For example, if you consider that the month-distance from 28/4/2000 and 1/5/2000 is 0 rather than 1 , then you can use: (date2.year - date1.year) * 12 + date2.month - date1.month - (date2.day >= date1.day ? 0 : 1) Give a try to ((date2.to_time - date1.to

Where to override current_user helper method of devise gem

浪子不回头ぞ 提交于 2019-11-28 17:04:00
How can i override current_user of devise gem. Actually I need to add web services for mobile-app. Currently devise is managing session and 'current_user' for web-application. Now Mobile app will send user_id to the server. I need to override current user like this def current_user if params[:user_id].blank? current_user else User.find(params[:user_id]) end end Should I need to modify devise gem as plugin ? or something else ? Kindly explain in detail as I am new in rails. Kind regards, According to the module Devise::Controllers::Helpers , current_user (together with all other devise helpers)

Rails: Force empty string to NULL in the database

无人久伴 提交于 2019-11-28 16:58:40
Is there an easy way (i.e. a configuration) to force ActiveRecord to save empty strings as NULL in the DB (if the column allows)? The reason for this is that if you have a NULLable string column in the DB without a default value, new records that do not set this value will contain NULL, whereas new records that set this value to the empty string will not be NULL, leading to inconsistencies in the database that I'd like to avoid. Right now I'm doing stuff like this in my models: before_save :set_nil def set_nil [:foo, :bar].each do |att| self[att] = nil if self[att].blank? end end which works

Rails 3.1 is very slow in development-mode because of assets, what to do?

笑着哭i 提交于 2019-11-28 16:50:28
After I added Sprockets, Rails is loading very slow in development mode, what should I do to speed it up? Take a look at https://github.com/wavii/rails-dev-tweaks . Rails is running all of the to_prepare hooks on every Sprockets asset request in development mode. This includes things like auto-(re)loading your code, and various gems sneak work in there too. rails-dev-tweaks disables to_prepare & reloading on any asset request (and a few others - read the first part of its README ). Speeds up your dev environment by a huge amount for any decently sized project. It's also configurable to do this

When to use self in Model?

强颜欢笑 提交于 2019-11-28 16:47:34
Question: when do I need to use self in my models in Rails? I have a set method in one of my models. class SomeData < ActiveRecord::Base def set_active_flag(val) self.active_flag = val self.save! end end When I do this, everything works fine. However, when I do this: class SomeData < ActiveRecord::Base def set_active_flag(val) active_flag = val save! end end The active_flag value doesn't change, rather it fails silently. Can someone explain? I can't find any duplicates, but if someone finds one that's fine too. DVG When you're doing an action on the instance that's calling the method, you use

Does form_tag work with Simple_form?

主宰稳场 提交于 2019-11-28 16:40:35
I have a form that is using form_tag and not sure how to use it with the simple_form gem . This is how my form looks: <%= form_tag create_multiple_prices_path, :method => :post do %> <% @prices.each_with_index do |price, index| %> <%= fields_for "prices[#{index}]", price do |up| %> <%= render "fields", :f => up %> <% end %> <% end %> <%= submit_tag "Done" %> <% end %> Can it be done? How would a form_tag change to use simple_form correctly? What about when using it with fields_for ? A Newbie could use some help. Thank you. simple_form is a wrapper around form_for , not form_tag . You can use

Rails: How to find_by a field containing a certain string

我与影子孤独终老i 提交于 2019-11-28 16:38:55
问题 I have a model named Topic, that has a name as a field. So say I have a term I'm searching for, apple. If I do a Topic.find_by_name("apple") I get a record back with the name apple. That's good -- but how do I change find_by_name so that it can find "apple juice" as well as "apple" -- basically, find names that contain the original query or exactly match the original query? Edit: Thanks for all the response. I guess I should've been a little more clear earlier, but what if I want to find by a

Upgrading from Rails 3 to Rails 3.1 [closed]

独自空忆成欢 提交于 2019-11-28 15:44:52
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . How do you upgrade from Rails 3 to Rails 3.1 beta? 回答1: This is what worked for me when updating an existing rails 3.0.8 project. Your mileage may vary... Update the rails version specified in my Gemfile to use the latest release candidate: gem 'rails', '3.1.0.rc4’ Update the bundle: bundle update Then update

How to make Rails 3.1 use SASS (Over SCSS) as the default?

送分小仙女□ 提交于 2019-11-28 15:42:44
Having a hard time figuring out how to make SASS, not SCSS, as the default for stylesheets. I've tried making a sass_config.rb file with this: Sass::Plugin.options[:syntax] = :sass Sass::Plugin.options[:style] = :compressed I've also tried adding that to the environment.rb file. Either way I get this error: .../config/environment.rb:7:in `<top (required)>': uninitialized constant Sass::Plugin (NameError) For rails 3.1.rc4, you could set the config: config.sass.preferred_syntax = :sass in the application.rb file I added the following to config/environments/development.rb : config.sass.preferred