ruby-on-rails

How to have an optional local variable in a partial template in rails?

I was thinking that at the top of my partial I would have something like this <% optional_width = default_value unless (defined? optional_width) But I've had inconsistent results with this, I'm thinking this is not a good way to do this. What is the "correct" way to do this in rails? ...

Why is Rack::Cache not hitting cache when etag matches?

Starting with no cache on server or client FIRST REQUEST GET /post/1 HTTP/1.1 HTTP/1.1 200 OK Date: Fri, 05 Mar 2010 09:05:46 GMT Last-Modified: Thu, 04 Mar 2010 21:00:08 GMT X-Rack-Cache: miss Etag: "c226165d5817af7c91592dab0bc0ac63" Cache-Control: max-age=3600, public The Cache is missed and Rails gets hit and queries the database...

Save after delegation

When using the delegation feature of Ruby in Models: class A def foo ... end def foo=(arg) ... end end class B < A belongs_to :a delegate :foo, :foo=, :to => :a end How can I force B to save A after setting b.foo="what_ever"? Oh and a.save should be called only if b.save is called and b.foo= was used - just ...

Rails routes - show action as root

I'm looking to make a show action of one of my controllers the root. I can easily do this: map.root :controller => 'articles', :action => 'index' When I go to localhost:3000/ it lists all the articles - that's great! What I want to achieve, though, is a URL like this localhost:3000/1 To display an article with the id 1. Changing th...

How to redirect to a 404 in Rails?

I'd like to 'fake' a 404 page in rails. In PHP, I would just send a header with the error code as such: header("HTTP/1.0 404 Not Found"); How is that done with rails? Thank you. ...

Is it possible to set different SASS output style for development and production with Compass in rails?

Let say I would like to set nested style for development and compressed for production. There is only one option in Compass configuration file: output_style = :compact # or :nested, :expanded, :compressed ...

How to load a session by its id in rails?

Im having a hard time trying to figure out how to load a session by its id. I don't want the current_user session, I need to load another one (mostly because flash doesnt share sessions with the browser). So I'm passing the session_id forward with the parameters, how do I get the session in the other side? Authlogic is redirecting me to ...

Stay in the bottom of the page. Rails, Ajax

Hello. I hava a form (form_remote_tag) that updates a comments section. The form is in the bottom. I would like to keep the page scrolled to the bottom so the text box is always visible. How can I achieve this? Thanks ...

file_column not creating files, just empty tmp directory

Up to the end of December 2009 everything worked fine, so i assume that after some upgrades on servers (Ubuntu 8.10 and second Ubuntu 9.10) something stopped working. Model: class Product < ActiveRecord::Base belongs_to :category file_column :thumbnail, :magick => { :geometry => "150x100>" } there is of course table 'products' in d...

How Create Post Request with redirect like a Form Submit

How can in controller redirect with post same data like this do form <form action="http://example.com/" method="POST"> <input type="hidden" name="q" value="a"> </form> I can't use redirect_to, because there is no support post data. ...

How to Mass Assign Then Edit Individually

Background: I have an application with athletes, each of these athletes (Athlete model) can be assigned workouts (Workout model, workout_assignment through model). Problem: At first I thought that using a through connection with athletes having many workouts through workout_assignment and workouts having many athletes through workout_as...

has_many :through and FormBuilder.fields_for

I have a class Bar that has a user-defined list of config keys and values, defined like this: class Bar < ActiveRecord::Base has_many :config_keys, :through => Foo has_many :config_values end So the available config keys come from the Foo class and the values for those come from the Bar class. I'm creating a form for this Bar...

How do I make a ruby gem available to my scripts?

On my web host's system, installing and using gems seems to require installing my own copy of ruby. With their help on a forum, I've gotten that done. I'm trying to use a particular gem (called Image Science) in a Rails app. At this point, if I open irb and type the following, this is what I get: require 'rubygems' #true require 'image...

I am trying to deploy my first rails app using Capistrano and am getting an error.

My deployment of a rails app with capistrano is failing and I hoping someone can provide me with pointers to troubleshoot. The following is the command output andrew@melb-web:~/projects/rails/guestbook2$ cap deploy:setup * executing `deploy:setup' * executing "mkdir -p /var/www/dev/guestbook2 /var/www/dev/guestbook2/releases ...

Ruby on Rails: Is there a way to find with ActiveRecord that will first try the cache and if it fails run a query?

Let's say that I'm looking for a specific item in a has_many association. Is there a way to find that won't rehit the database if what I'm requesting is already cached? Here's my situation (which is not working and does not do what I requested): class User has_many :friendships def some_function(buddy) f = friendships.detect{...

Rails f.error_messages alwais empty

What did I do :)? In rails form has f.error_messages alwais empty. How can I fix/check this? Thx ...

How to use Cucumber in forms with custom labels

I'm trying to learn how to use cucumber and got this issue: I have a form that as: <p> <%= f.label :name, "Nome" %><br /> <%= f.text_field :name %> </p> And in my cucumber feature I have: And I fill in "name" with "Reitoria do Porto" This make the test fail with: And I fill in "name" with "Reitoria do Porto" ...

RAILS: I've got an action that calls itself, when the form of the action's view is being sent, now I want to rout out of that action...

Hi folks, this time it's about Ruby On Rails. I have a form in the view "progress.html.erb" and an action called "progress". When the form is sent, the action "progress" is called again. This must be like that, because I'm asking the user several questions by an automatic system. Then, when the questioning is finished and the user is ...

Routes problem with namespaces in a Rails Engine based plugin

Hi! I'm trying to create a dynamic interface. Where my model classes exist and my controllers are dynamically created when launching the application. Everything happens in my routes file where the resources are created! ActionController::Routing::Routes.draw do |map| map.namespace :admin do |admin| TestAdmin.models.each do |m| ...

What does map(&:name) do in this Ruby code?

Trying to understand Ruby a bit better, I ran into this code surfing the Internet: require 'rubygems' require 'activeresource' ActiveResource::Base.logger = Logger.new("#{File.dirname(__FILE__)}/events.log") class Event < ActiveResource::Base self.site = "http://localhost:3000" end events = Event.find(:all) puts events.map(&:name...