ruby-on-rails

Restful commands

I am new to RESTful stuff. But, I want to use it in my rails app. When I add this to my routes.rb map.resources :notes I get routes to these methods created: index create new edit show update destroy What I am wondering is what is the difference between edit/update and create/new? Is there any standard definitions of how these met...

Rails & Windows

Is Rails development really this hard on Windows? I'm a PHP developer looking forwards to using Rails (mainly because every single PHP framework I've tried has some quirk that I just hate). I downloaded Aptana Studio (w/ RadRails) as it seemed to be a good solution (and because I love anything Eclipse-based). But that's where the proble...

ActionController::MethodNotAllowed after reloading routes?

Hi, I have an application where I'm dynamically loading routes by a model, and calling ActionController::Routing::Routes.reload! after creating/updating that model. The problem is that after doing this, I'm receiving the following error when I try to hit that new route: ActionController::MethodNotAllowed Only get, head, post, put, and...

Best practice for limiting the number of associations within a has_many relationship?

Say that I have two models- Users and Accounts. Each account can have at most n users associated with it, and a user can only be associated with one account. It would seem natural to say that User belongs_to :account and Account has_many :users However, I'm not clear on the best practice when it comes to limiting the number of ...

Rails non-image file upload to DB without using server-side temp files?

I'm looking into the feasibility of adding a function to my Rails-based intranet site that allows users to upload files. Two purposes: My users are widely distributed geographically and linking to documents on the shared network storage doesn't always work (different addresses, DNS entries and stuff outside my control or interest) so I'...

How can I access a different object collection in a view in Rails?

I'm not sure how to search for this answer, so I'll go ahead and ask it. In my rails project I have a User model and a foo model. A user can have one or more foo models assigned to it. I have accomplished this by adding has_many :foo, :through => :user_foo in my user model. Now, over in my view, I want to display a list of all foo...

Using scope_builder to conditionally build a named scope in an Active Record model search() method

I'm using Ryan Bates' excellent scope_builder to conditionally build a new named scope to use in a search() method of an Active Record model. The example documentation shows that you can do something like the following: # in product model def self.search(options) scope_builder do |builder| builder.released.visible b...

How do I run Ruby tasks that use my Rails models?

I have a Rails app with some basic models. The website displays data retrieved from other sources. So I need to write a Ruby script that creates new instances in my database. I know I can do that with the test hooks, but I'm not sure that makes sense here. I'm not sure what this task should look like, how I can invoke it, or where it sh...

:id in URLs

I'm still new to ROR, so pardon the simplicity of the question... So http://www.example.com/controller/:id displays a record in my table, with :id being a number (1,2,3 etc.). Is there a way I can have :id in the URL be the value of a field in the displayed record? Such that I can have http://www.example.com/controller/record_field ??...

Custom Ruby Routes in Model

Okay, these two related questions are in reference to Railscast #21: I'm having some trouble with routes. Two issues: 1) The routes in the tutorial seem to be relative to the root of the application; I want them to be relative to the root of the model. So "http://example.com/login" I need to be "http://example.com/model/login" (and ...

Save multiple objects to single field in a table

I want to save a set of email addresses collected from a text field (separated by commas) into a single field in a table. I am trying to use serialize but I'm getting SerializationTypeMismatch error. How can I do this? This is my model: class Crew < ActiveRecord::Base class Recipients < HashWithIndifferentAccess ; end serialize :reci...

Can I unit test the DOM?

Hello, We have a Ruby on Rails app that uses some javascript in the views. Since we have good test coverage on the rest of the app, we want to continue that here. The js we use manipulates the DOM to show/hide divs and perform incremental searching for a combobox selecting from a table. Does anyone know of ways to test DOM manipulati...

What's the "h" mean in "<%=h"?

When I generate a default scaffold, the display tags on show.html.erb have <%=h @broker.name %> I know the difference between <% and <%=. What's the "h" do? ...

Rails Plugin: Paperclip Question

How can I prevent the image tag that calls the associated image from displaying if no image is associated with the record? <%= image_tag @agent.avatar.url %> ...gives me the text "Missing" if there is no image associated with that agent. I want to test to see there is an image available first, then render the above tag if the test re...

Rails: Check has_many in View

If I have... class Bunny < ActiveRecord::Base has_many :carrots end ...how can I check in the View if @bunny has any carrots? I want to do something like this: <% if @bunny.carrots? %> <strong>Yay! Carrots!</strong> <% for carrot in @bunny.carrots %> You got a <%=h carrot.color %> carrot!<br /> <% end %> <% end %> I kn...

Tabs, Text-Mate, Editing HTML (Rails)

Is there any way to force Text-Mate to use a two-space tab instead of a full tab when editing HTML (Rails) documents? ...

What caching strategy would best suit this problem?

Hi, First of all, let me say I am very new to rails, have been working with it for only a couple of days. My first application is a little different from traditional: on one of my model classes I have parts of data that is pulled from a database and other part that is acquired by doing an HTTP REST request to an external resource. I h...

How to compress http responses using mongrel

I have a rails app that uses heavily js (over 1MB total). I'd like to compress them in order to reduce the overall load time of the site. I did a fast search and found that most browsers accept compressed content. I'd like to know what can I do to make my rails app send compressed content and thus make the user experience better. ...

Capistrano - RAILS_GEM_VERSION Error

I can't figure this out. Capistrano keeps giving me this error: ** [out :: myserver.net] Missing the Rails 2.1.1 gem. Please `gem install -v=2.1.1 rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed. S...

Where is the correct place to initialize the DRb service within a Rails application?

I'm using DRb within a Rails application to offload an expensive task outside the Rails process. Before initializing the client stub with DRbObject.new it is necessary to initialize the DRb service with DRb.start_service. Doing this in model or controller appears to leave threads in an uncertain state. When I exit mongrel it says: Reap...