ruby-on-rails-3.1

Iterating the dates in ruby on rails

﹥>﹥吖頭↗ 提交于 2020-01-25 12:13:05
问题 I am developing an application for reporting and analytic,where I need to generate data on a daily basis and put it in a CSV.To be more clear, if I take the report today like day one any how I can get the report thats already done, if I get the report tomorrow I should be able to get the data of today,yesterday. If i take after 4 days I should get the report starting from day 1 to day 4. How it can be done. Looking for heads up 回答1: How about this: today = Date.today tomorrow = today.next

No Javascript runtime on windows when running rails server

☆樱花仙子☆ 提交于 2020-01-25 02:21:26
问题 I'm completely new to Ruby on Rails, and I'm having trouble setting it up on my Windows PC . I have successfully followed the instructions on http://rubyonrails.org/download. However, when I go to run the rails server command I come up with this output: C:/Ruby192/lib/ruby/gems/1.9.1/gems/execjs-1.2.9/lib/execjs/runtimes.rb:47:in `a utodetect': Could not find a JavaScript runtime. See https://github.com/sstephen son/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable) from C:

missing gems can

回眸只為那壹抹淺笑 提交于 2020-01-24 20:06:09
问题 Bundler could not find compatible versions for gem "railties": In Gemfile: rails (= 3.2.3) ruby depends on railties (= 3.2.3) ruby jquery-rails (= 2.0.0) ruby depends on railties (3.2.5) I have got that error message while trying to "bundle install" and here is my gemfile source 'https://rubygems.org' gem 'rails', '3.2.3' # Bundle edge Rails instead: # gem 'rails', :git => 'git://github.com/rails/rails.git' group :development do gem 'sqlite3', '1.3.5' end gem 'json' # Gems used only for

respond_to only format.js for all mime types

孤街浪徒 提交于 2020-01-24 06:57:11
问题 I have a controller which respond_to format.js, however, most request assume the old format.html still exists and throws a 404 exception. How do I capture all MIME request on the controller and redirect them only to format.js? Here's the current controller action def search respond_to do |format| unless @search.nil? format.js { render :partial => '/search/search_form', :status => 200 } else format.js { render :partial => '/search/not_exist', :status => 500 } end end end I'm trying to do

Change locale at runtime in Rails 3

a 夏天 提交于 2020-01-23 03:21:10
问题 I am working on a rails 3 app which has different languages in my locales folder. The files are en.yml, pu.yml, sp.yml. All languages have to be converted to their various format and I need help in making users chose any language of their choice with a link like <%= link_to "English language", ...%> <%= link_to "spanish", ...%> When a user choses a language, the language is set as the user's preferred language so that the user does not have to keep selecting a language after each login. 回答1:

How to rescue exception central and DRY?

…衆ロ難τιáo~ 提交于 2020-01-23 01:02:09
问题 I have an exception that is produced at ~20 seperate places. It can be rescued easy and in the same way at every place but thats not dry and eaven a crapy work! I want to rescue this exception at a central position. How can I arrange this? Its about the ActiveRecord::RecordNonUnique exception by the way,... 回答1: What about this ? def rescue_from_record_non_unique yield rescue ActiveRecord::RecordNonUnique # your code end # ... rescue_from_record_non_unique do # do something end 来源: https:/

Rails form_for with file_field and remote => true and format => :js

做~自己de王妃 提交于 2020-01-21 12:22:09
问题 I am submitting a form_for with a file_field in it. When the form is submitted with a post, the controller responds with a .js file which will be executed. The file_field is optional. When I upload a file, the js file is opened in a new document (as a simple text file) and the js is not executed. When I don't upload any file, the response div is updated. I want the update success to be notified in the response div or a failure message independent of the file upload. May be this is done for

Rails importing CSV fails due to mal-formation

感情迁移 提交于 2020-01-21 08:49:27
问题 I get a CSV:MalFormedCSVError when I try to import a file using the following code: def import_csv(filename, model) CSV.foreach(filename, :headers => true) do |row| item = {} row.to_hash.each_pair do |k,v| item.merge!({k.downcase => v}) end model.create!(item) end end The csv files are HUGE, so is there a way I can just log the bad formatted lines and CONTINUE EXECUTION with the remainder of the csv file? 回答1: You could try handling the file reading yourself and let CSV work on one line at a

Rendering a partial in assets

百般思念 提交于 2020-01-21 03:15:28
问题 I'm using Ruby on Rails 3.1 and I'm wondering how I could render a partial in a javascript asset. What I'm aiming at: # in /app/assets/javascript/cart.js.coffee.erb $('a.add_sth').click -> $('.random_container').append('<%= render partial: 'way/to/partial' %>') This causes a NoMethodError: undefined method `render' for #<#<Class:0x007fc54584c6e8>:0x007fc5474cd470> If I write <%= 2+3 %> instead it works fine, btw. I think the problem is that the asset pipeline is independent from the default

“Uninitialized constant” error when including a module

寵の児 提交于 2020-01-20 13:38:53
问题 I am trying to reference an association extension but it errors with: NameError (uninitialized constant User::ListerExtension): app/models/user.rb:2:in `<class:User>' Here is my implementation: app/models/user.rb class User < ActiveRecord::Base include ListerExtension has_and_belongs_to_many :roles, :uniq => true, :extend => Lister lib/lister.rb module ListerExtension def lister self.map(&:to_s).join(', ') end end I am using Rails v3.1.3. 回答1: Andrew Marshall has an excellent point about the