ruby-on-rails-3

Rails 3 trouble with namespaces & custom classes (uninitialized constant)

旧巷老猫 提交于 2020-01-01 17:11:14
问题 I have a file in my Rails 3.2.11 project called app/queries/visible_discussions.rb which looks like the following: class VisibleDiscussions ... end I'd like to namespace the query so that I can call it using something like Queries::VisibleDiscussions so I tried to do the following: module Queries class VisibleDiscussions ... end end However, I'm getting a uninitialized constant Queries (NameError) when I try to call Queries::VisibleDiscussions from the rails console. Any ideas? 回答1: if you

Arrays frozen in Rails 3.2.4

℡╲_俬逩灬. 提交于 2020-01-01 17:06:16
问题 I recently updated a rails app from 3.0.4 to 3.2.4 and I have a strange problem. It seems every array in my app has become frozen. For example if an array gets a set of articles from the database, that array will be instantiated at the first call of the method and will not change. If an article in the array is deleted in the database, it will still refer to it in the array. It only changes if I restart the server. However, when I call array.frozen? it returns false. And when I deploy to

How to call helper method from model?

廉价感情. 提交于 2020-01-01 16:48:09
问题 I use MongoDB as database in my Rails application with MongoID gem. I want to call helper method from model with n aafter_create callback method. How is it possible? My model code is: class Department include ApplicationHelper after_create :create_news private def create_news @user = ApplicationHelper.get_current_users end end And my helper code is: module ApplicationHelper def get_current_users current_user end end When I create new department then following error occur. undefined method

Rails 3, HABTM how to query with conditions

眉间皱痕 提交于 2020-01-01 16:42:10
问题 I have Awards and Categories, joined with Awards_Categories I have 2 Categories. I need to find all the Awards that have Category.id = 1, but not Category.id =2. Some categories have one or the other, some have just one. I want a list of the Awards that have category 1 but not category 2. I have a scope: scope :in_categories, lambda { |categories| joins(:categories). where(:awards_categories => { :category_id => categories } ). select("DISTINCT awards.*") } And this works with a query like:

How to get fragment expiration date in Rails 3.1?

别说谁变了你拦得住时间么 提交于 2020-01-01 16:41:46
问题 I need to get expire or creation time for cache entry. This is for: sitemap index, each sitemap in index is cached action. I want to add <lastmod> attribute, which, in my case, will be cache entry creation time. For example for action caching: class ProductsController < ActionController caches_action :index def index @products = Product.all end end I need something like this: Rails.cache.get(:controller=>'products',:action=>'index').created_at 回答1: I found solution, which work with all

:remote => true/data-remote on a form loaded via ajax

人走茶凉 提交于 2020-01-01 14:36:45
问题 In my Rails app, I have a form that is loaded via Ajax using jQuery load method. function load_sales_form(product_id) { $("#sales_form").load("<%= url_for(:action => :show_sales_form) %>"/ + product_id); } The loaded form has a form_for tag with the :remote => true option and it does add the data-remote="true" attribute to the form. But the form isn't submitted using Ajax when the user clicks the submit tag button. It works fine if the form is loaded in the standard, non-ajax way, but it the

:remote => true/data-remote on a form loaded via ajax

可紊 提交于 2020-01-01 14:36:30
问题 In my Rails app, I have a form that is loaded via Ajax using jQuery load method. function load_sales_form(product_id) { $("#sales_form").load("<%= url_for(:action => :show_sales_form) %>"/ + product_id); } The loaded form has a form_for tag with the :remote => true option and it does add the data-remote="true" attribute to the form. But the form isn't submitted using Ajax when the user clicks the submit tag button. It works fine if the form is loaded in the standard, non-ajax way, but it the

Carrierwave and s3 with heroku error undefined method `fog_credentials='

[亡魂溺海] 提交于 2020-01-01 14:23:14
问题 I'm trying to setup carrierwave and s3 with heroku. I'm following the carrierwave docs exactly: https://github.com/jnicklas/carrierwave I've setup a bucket named testbucket in AWS, then I installed fog and created a new initializer with this inside : CarrierWave.configure do |config| config.fog_credentials = { :provider => 'AWS', # required :aws_access_key_id => 'my_key_inside_here', # required :aws_secret_access_key => 'my_secret_access_key_here', # required :region => 'eu-west-1' # optional

Creating many-to-many relationships on a nested rails form

旧街凉风 提交于 2020-01-01 14:07:13
问题 I am trying to create Groups, Users and Memberships (the many-to-many relationships) simultaneously. People can add users to the group as they create it and then I want it to route through to a view of the group with all the members. I can get the users to be created and the current user's membership to save to the database. However I'm struggling to get the id for the freshly created User objects in order to save the new memberships. Group.rb class Group < ActiveRecord::Base has_many

Using the Bubble sort method for an array in Ruby [closed]

折月煮酒 提交于 2020-01-01 12:00:08
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . I'm trying to implement the Bubble sort method into an easy coding problem for Ruby, but I'm having some trouble. I understand the idea is to look at the value of the first element and compare it to the value of the second element and then swap them accordingly, but I can't seem