ruby-on-rails

How to get a variable to have 2 decimals

I have a variable i would like to force to have 2 and always 2 decimals. Im comparing to a currency. Often i get a comparison looking like the following. if self.price != price #do something end Where self.price = 120.00 and price = 120.0. The self.price is set with a :precision => 2 in the model, but how do i do the same with a var...

SQL: Get a selected row index from a query

Hi everyone! I have an applications that stores players ratings for each tournament. So I have many-to-many association: Tournament has_many :participations, :order => 'rating desc' has_many :players, :through => :participations Participation belongs_to :tournament belongs_to :player Player has_many :participations has_ma...

Ruby on Rails - generating bit.ly style uuids

Hello, I'm trying to generate UUIDs with the same style as bit.ly urls like: http://bit.ly/aUekJP or cloudapp ones: http://cl.ly/1hVU which are even smaller how can I do it? I'm now using UUID gem for ruby but I'm not sure if it's possible to limitate the length and get something like this. I am currently using this: UUID.generat...

Automate localization of Date and Time functions

Hi all, is there any way to configure a rails application so that all Date/Time outputs are automatically localized? That means: <%= model.created_at %> equals to <%=l model.created_at %> ...

Automatically build a nested model form with using the 'link_to_add_fields helper'

Ive created a nested model form the RailsCasts Nested Model Form Part 2 example and have it working with Rails 3 and JQuery. Currently, a user can create a new project and click a link to add new tasks, and within each new task, click a link that will allow them to add new assignments to each task. Here's the link to create a new task ...

Active merchant and Paypal API for recurring payment

Hi everyone, I have few questions on paypal's recurring payments, I went trough the paypal documentation to find some answers but, it didn't help me much. Here are the stuffs that I need to know, hope someone can help me on these. I'm using paypal's standard payment account. So far I managed to create recurring payments with paypal by ...

returning an array that contains an array and hash in ruby method?

HI is it possible to return an array that contains an array and hash from a method in ruby? i.e def something array_new = [another_thing, another_thing_2] hash_map = get_hash() return [array_new, hash_map] end and to retrieve the array: some_array, some_hash = something() thanks ...

Installing Heroku on Ubuntu Lucid Lynx is broken

I am trying to get a Ruby on Rails app hosted free somewhere, and Heroku is looking like my last resource. It is supposed to work on Linux, and the gem installs with no errors, but whenever I run any Heroku command it spits out several errors, all connected, and talking about a failed 'require.' I looked it up in the code, and it says: ...

respond_to change response type

Hi! I am calling a controller's create action via ajax so when the object saves successfully, the js response is triggered. However, if the object fails to save due to validation, then I want the response to be html. I hope to achieve this by not returning the js response in the else block (please see code below) but this produces a 406...

Ruby on Rails 3 - Reload lib directory for each request

Hey guys, I'm creating a new engine for a rails 3 application. As you can guess, this engine is in the lib directory of my application. However, i have some problems developing it. Indeed, I need to restart my server each time I change something in the engine. Is there a way to avoid this ? Can I force rails to completely reload the ...

Prevent Rails from caching a password

I'm setting up an edit profile page. And the password is available for change on it. The only problem is that the password_field gets prepopulated with the password, and when they just want to change their name, they must also change their password as well. Does anyone know a common workaround to this? ...

Ruby on Rails named routes with absolute path from application root

I'm trying to set up named routes for page: www.myhost.com/blog/about (map.about '/about', :controller => 'page', :action => 'about') And I have another route for a resource: www.myhost.com/blog/post/3 (map.resources :posts) Now I don't know how should I link to "about" page. If I use a) <%= link_to 'About', about_url %> or ...

Rails: Overriding const_missing within a module

Within my Rails application I have a module defined this way: module WhateverModule class WhateverClass ... end end This file (whatever_class.rb) is located under /app/models/whatever_module const_missing is being overriden by Rails and despite I did some workarounds, involving initializers, I wish I could make it in a better...

Another rails nested form question..

Trying to perform a nested object form. The page loads with no errors, but when I send it, no information gets saved to the organization model. The SQL call says this .. Parameters: {"commit" => "save", "action"=>"update","_method"=>"put", "organization"=>{"likes_snacks"=>"0"}, .. Which is right. The 1 and 0 can be changed properly ...

Rails: best way to test an action called remotely?

I was trying out Rails again, this time the 3 version, but I got stuck while writing tests for an action that I only call remotely. A concrete example: Controller class PeopleController < ApplicationController def index @person = Person.new end def create @person = Person.new(params[:person]) @person.save end end ...

Customize Rails Hash.from_xml with custom conversion

I want to convert my XML document to Hash in Ruby/Rails. Actually, the default conversion by Hash.from_xml in Rails works for me except in one case. I have a list of items contained in <item-list> element, these items can be of different types though. For instance, standard-item and special-item, each of which has different set of child...

rails comparing values of params[:id] and session[:user_id] not working

I'm new to rails after moving from PHP and am having no end to the frustrations, but hopefully there is a steep learning curve. I was following a guide on how to make a twitter clone in rails, and have continued along that path making it more and more twitter like. So I've got a 'users' page /users/show.html.erb which show all the pos...

find class methods for every model.

How may i find class methods of the specific model. Like instance method ModelName.instance_methods ...

Installing MSSQL Adapter for Ruby on Rails fails

I run this command: gem install activerecord-sqlserver-adapter --source=http://gems.rubyonrails.org and it fails with ERROR: Could not find a valid gem 'activerecord-sqlserver-adapter' (>= 0) in any repository How can I install the Microsoft SQL Server adapter? ...

Omit HTML but keep <br> tags in rails

I need to display user comments, omitting HTML to prevent attacks (when custom styled elements can be posted as comments) The only thing, i would like to keep by displaying - is tag I displaying the comment in this way: <p class="content"><%=h comment.content.gsub(/\n/,"<br/>") %></p> Comment is suppossed to be saved in database wit...