ruby-on-rails-3.2

Write controller and feature specs for ActiveAdmin using RSpec?

送分小仙女□ 提交于 2019-12-02 21:16:09
How does one write a controller and feature spec for the following ActiveAdmin code: # app/admin/organization.rb ActiveAdmin.register Organization do batch_action :approve do |selection| Organization.find(selection).each {|organization| organization.approve } redirect_to collection_path, notice: 'Organizations approved.' end end Here is my feature spec. It cannot find 'Batch Actions' which ActiveAdmin loads in the pop-up menu. # spec/features/admin/organization_feature_spec.rb require 'spec_helper' include Devise::TestHelpers describe 'Admin Organization' do before(:each) do @user =

Override ActiveRecord << operator on has_many :through relationship, to accept data for the join model

╄→гoц情女王★ 提交于 2019-12-02 21:12:41
I have three classes: Person, Position, and Directory. A Person has_many :directories, :through => :position. A Directory has_many :people, :through => :position. Both Person and Directory has_many :positions. The Position model, in addition to having an id, a person_id, and a directory_id, has one or more additional fields (e.g., title). What I would like to be able to do is add data to the join model, such as the title field, every time I add a person to a Directory.people collection. The usual << operator won't cut it. Id est: directory = Directory.last # Let's assume that retrieves a

Scope that has three levels deep joins

廉价感情. 提交于 2019-12-02 20:52:54
问题 My Program table has many Measures My Measure table has many Targets My Target table has a column called " money " My ActiveRecord query looks like this: @programs2 = Program.includes([measures: :target]).where('organization_id = 1').limit(2) I want to define a scope such that the query can return top Programs that their target.money value is the lowest. So I need to write a scope and apply it to that query but How and Where in the model should I define that scope, something like this? Well

Possible to alias a belongs_to association in Rails?

喜你入骨 提交于 2019-12-02 19:59:32
I have a model with a belongs_to association: class Car < ActiveRecord::Base belongs_to :vendor end So I can call car.vendor . But I also want to call car.company ! So, I have the following: class Car < ActiveRecord::Base belongs_to :vendor def company vendor end end but that doesn't solve the assignment situation car.company = 'ford' , so I need to create another method for that. Is there a simple alias mechanism I can use for associations? Can I just use alias_method :company, :vendor and alias_method :company=, :vendor= ? No it doesn't look for company_id for instance change your code as

Integrating CKEditor with Rails 3.2

半腔热情 提交于 2019-12-02 19:49:57
Similar to Integrating CKEditor with Rails 3.1 Asset Pipline I am trying to integrate ckeditor with my rails 3.2 application. I have all ckeditor files copied under /app/assets/javascripts/ckeditor/* . I have the following lines in my application.js and application.js is included in my layout file: //= require jquery //= require jquery_ujs //= require ckeditor/ckeditor //= require_self Taken it from the answer to Integrating CKEditor with Rails 3.1 Asset Pipline I can understand that I need to add something like: config.assets.precompile += your_files to my development.rb file so that all the

Using question mark character in Rails/ActiveRecord column name

跟風遠走 提交于 2019-12-02 18:49:10
In keeping with Ruby's idiom of using a question mark in boolean methods (e.g. person.is_smart? ), I'd like to do the same for an ActiveRecord field in Rails: rails generate model Person is_smart?:boolean I haven't actually run the above statement. I assume that database fields can't have a question mark in them. Will rails deal with this appropriately? Is the best practice to simply leave question marks off of models? Using Rails 3.2.8 cdesrosiers Rails will automatically generate the method smart? if there is a field named 'smart' . One "gotcha" to be aware of if you happen to use :enum in

making a pie-chart of the user age in rails

元气小坏坏 提交于 2019-12-02 17:50:59
问题 I have this function in my User model that calculates the User ages def get_age now = Time.now.utc.to_date now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1) end I want a rails statement that yields a query against the users using this function and returns their age numbers. Then the ages are grouped in labels such as [below 10, between 10-20, etc] something like: <%= pie_chart User.group("get_age"), {library: {title: "User's Age"}} %>

How to deal with vendor/plugins after upgrading to rails 3.2.1

青春壹個敷衍的年華 提交于 2019-12-02 17:41:22
After upgrading to rails3.2.1,this warning occurs: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released I move my plugins in vendor/plugins directory but i don't know how to write config/initializers/myplugin.rb file, and google can't find the answer. coneybeare I just wrote a blog post on this: How

Why is my Rails mountable engine not loading helper methods correctly?

淺唱寂寞╮ 提交于 2019-12-02 17:38:18
I've built a rails gem that mounts as an engine. The engine is scoped to it's own namespace. In the engine, there's an MyEngine::ApplicationHelper module which adds a bunch of view helper methods. In my application layout, I refer to some of these methods. When I first load any of the pages in development mode I get a NoMethodError , complaining that the method (defined in the gem's ApplicationHelper ) doesn't exist. Once I edit ApplicationController within my app, the problem corrects itself. Something tells me this is down to the recent changes in Rails's auto-loading; I'm using Rails 3.2.2

How to do render partial on jQuery ajax success method with rails 3

非 Y 不嫁゛ 提交于 2019-12-02 16:52:52
I'm using rails 3.2.1 with jQuery for an ajax call. My jQuery code is : jQuery.ajax({ url: "/org_pages", data: 'org_id='+ org_id, type: "POST", success: function(result){ jQuery("#image_center").html("<%= escape_javascript(render(:partial => 'pages/top_link')) %>"); }, error: function(){ alert('Error occured'); } }); My problem is on the web page the output is showing this : <%= render :partial => 'pages/top_link', :collection=>@pages %> How it should display my render partial page. :( try by rendering the partial from the controller as, def method_name render :partial => 'some_partial' end