ruby-on-rails

Ruby on Rails ActiveRecord: pluralization

I'm new to Rails, so forgive my ignorance of ActiveRecord. One of my models is named "campus". I ran the migration and it pluralized everything except "campus". I thought that was lame so I added the code to the environment config to leave everything singular. I deleted the tables, manually edited the migration files to use singu...

Check if a string is capitalized in Rails

I'm looking to check if a string is all capitals in Rails. How would I go about doing that? I'm writing my own custom pluralize helper method and I would something be passing words like "WORD" and sometimes "Word" - I want to test if my word is all caps so I can return "WORDS" - with a capital "S" in the end if the word is plural (vs. "...

What is the difference between _url and _path while using the routes in rails

When we define routes in routes.rb using the name like map.some_link we can use the link in two ways- some_link_url() some_link_path. What are the differences between the two? Which is more secure to be used? ...

How can I use the same tokenizer across models with validates_length_of?

I have the following validation in a model: validates_length_of :description, :minimum => 2, :on => :save, :message => "must be at least 2 words", :tokenizer => lambda {|text| text.scan(/\w+/)} And this works fine. When I add a second field to the model that needs to be validated by number of words, I declare tokenize_by_words ...

Getting "unknown attribute: user" while using nested models

Hi, I'm getting an error when I submit the form http://pastie.org/846712 I tried changing "f.fields_for :users do |builder|" to "f.fields_for :user do |builder|", but it generated nothing. To give you a picture of how my accounts controller looks like: http://pastie.org/846714 And the error I'm getting is on: http://pastie.org/846715 T...

How can I check with cucumber & webrat & selenium that a selector/tag exists exactly two times in a response?

I used following step-definition with cucumber and webrat and everything worked fine: Then /^I should see "([^\"]*)" worker in the workerlist/ do |number| response.should have_selector("td.worker_name", :count=>number) end I have moved now to selenium and "somehow" the have_selector doesn't take a :count parameter anymore. I get fo...

aggregate conditional has_many rails association

I'm not sure if this is feasible or even preferable But I'd like to build a has_many relationship or a conditional named_scope type relationship to ease my pain into one simple relationship instead of two. Currently I have two models which basically contain the same information and belong to the same Model rooms thru the same key room_...

Ruby on Rails Forms: how to create a CheckBox Table (or List)

What's the best way to create a related set of checkboxes in Ruby on Rails? In the ToscaWidgets library used by Turbogears you can do the following: twf.CheckBoxTable('arbitrary_numbers', num_cols=5, options=['1','2','3','4','5','6','7','8','9','10']), This generates 10 labeled checkboxes in two rows of 5 checkbo...

cancan authlogic gem's edit current user profile error

Once I added the cancan Gem to my authlogic authorisation system, I started running into problems. My link_to edit current user profile died! code: #application.html.erb <%= link_to "Edit Profile", edit_user_path(:current) %> #ApplicationController private def current_user_session return @current_user_session if defined?(@cur...

Rails belongs_to testing

I have a Proposal model that belongs to Project: class Proposal < ActiveRecord::Base belongs_to :project has_many :articles, :as => :document, :dependent => :destroy has_many :sections, :through => :articles # proposal has project - test/unit/proposal_test.rb validates_presence_of :project_id end The route I set up to show ...

has_many through checkboxes

Given the following models class Feed < ActiveRecord::Base has_many :alerts , :dependent => :destroy has_many :users , :through => :alerts end class Alert < ActiveRecord::Base belongs_to :user belongs_to :feed has_and_belongs_to_many :alerttypes end class Alerttype < ActiveRecord::Base has_and_belongs_to_many :aler...

Dynamic dropdown search box

Are there any gems that would help me make a search box like the one for tags on stackoverflow? (Rails 2.3.5, required IE7 support and graceful no-script fall-back) P.S.: Do these boxes annoy you or do you think it's a good thing to have one? ...

Attachment_fu: can't disable :partition option

I'm trying to use the Attachment_Fu plugin in a Rails project, and want to customize the paths where uploaded files are saved. The documentation shows this option: :partition # Whether to partiton files in directories like /0000/0001/image.jpg. Default is true. (The 0001 part is an ID from a table.) I don't want that, s...

Rails Route question

I want to setup a route so that if the user goes to http://mysite.com/page.html it is routed to the controller page_controller and the action index. How would I do this? ...

Rails - named_scopes - conditional conditions

If I have the following named_scope named_scope :scope_by_user_id, lambda {|user_id| {:conditions => ["comments.user_id = ?", user_id]}} is there a way in rails to only apply that condition if user_id is not nil? ...

How to share code across models? (Rails 2.3)

I have a couple of methods I'd like to share between models rather than copying and pasting them. In a controller, I can do this by putting the methods in application_controller.rb which automatically gets included in every controller. Is there a similar thing for models? Thanks! ...

Testing rails controller with rspec

Hi, I'm new to rails and I'm trying to test a controller with rspec. My first test is when the show action is invoked, it should lookup a Category by url. The problem is when I add the stubbing code, I get the following error: undefined method `find' for # my test looks like this: require 'spec_helper' describe CategoriesController ...

Browser interpreting line breaks in code as spaces (HTML)

I have 6 images sized 50x50 that are supposed to fit in a 300px div (50 * 6 = 300). In my code I write them as follows: <img src="foo.png" /> <img src="foo.png" /> <img src="foo.png" /> <img src="foo.png" /> <img src="foo.png" /> <img src="foo.png" /> Note the line breaks in between the image tags. I write them that way for clarity. T...

Recursive routes in Rails

Is is possible to create a recursive route in Rails? I have an application, which allows a admin to create pages. The page model is a nested set and so each page has a parent_id hence the pages are structured in trees. The page model also uses the Friendly ID plugin to provide slugs for each page. When a user browses the site I would l...

Including calculations on related data in an ActiveRecord query

Let's say I have some models: User, Post, and Vote. A user has many posts and a post has many votes. Votes can be either an up vote or a down vote (stored as a boolean). What I am considering is the best way to do queries like these: All users, and the total number of up votes they've received. All users, and the post that has received...