ruby-on-rails

rake spec not using the rails environment

I'm attempting to use rspec in a rails project I've just upgraded to rails 2.3.2. I've installed rspec 1.2.6 and rspec-rails 1.2.6 as plugins in the app. My problem is the specs don't have access to my app classes or any of the rails standard libraries. First I had to specify the model class I want to test by using the full path from R...

in Ruby on Rails, does map.resources :stories immediately make Story RESTful?

I was reading Simply Rails by Patrick Lenz... maybe I missed something, it seems that whenever we put map.resources :stories in routes.rb then immediately, the controller will have special convention and now Story is a RESTful resource? Maybe the author used the word resource but didn't mention that it is RESTful but they are the sa...

in Ruby on Rails, why does "form_for @story" has to have Story as RESTful

It seems that when we use form_for @story do |f| then Story has to be a RESTful resource and it has to be map.resources :stories in the routes.rb. Can Story be not RESTful? Can it be a form for non-RESTful data? ...

Sort Objects by Boolean values in Ruby

My apologies if this has been answered before or is obvious...did some searching here and on the Goog and couldn't find an answer. I'm looking to sort an array of Providers by price and whether they are a preferred_provider? (true or false) For instance in array p of Providers... p1.price == 1, p1.preferred_provider? == false p2.price...

RESTful Authentication: Stumped trying to do simple redirect on cookie based authentication

I have a RoR application that's using the RESTful Authentication plug-in. Everything works great. I recently enabled cookie based authentication and that works fine too. The problem is that I want to change the default landing page when the user is authenticated using a cookie. I want to have a cookie authenticated user redirected to ...

Why use HTTP PUT and DELETE methods instead of POST?

new_story GET /story/new(.:format) {:action=>"new", :controller=>"stories"} edit_story GET /story/edit(.:format) {:action=>"edit", :controller=>"stories"} story GET /story(.:format) {:action=>"show", :controller=>"stories"} PUT /story(.:format) {:action=>"update", :controller=>"stories"} ...

Why are these default parameters defined as they are?

I'm currently learning Ruby and RoR and I stumbled across this declaration: link_to_remote(name, options = {}, html_options = nil) I discovered that this pattern is used on several other Rails functions. Why are the default values defined that way? Why not one of these two? ... options = {}, html_options = {}) ... options = nil, h...

After Rails console restart, parent model can't find associated child model objects (and then can)

I've been stuck trying to figure out why a counter cache on my (parent) BlogPosts table won't update from the (child) Comments table. At first I thought the answer provided in my earlier question might be the solution but something happened after I went to bed last night because when I woke up this morning and restarted my Rails console,...

Move Expired Items?

Hi guys, I have a model Post which has a expiry_date. I want to know what is the best way to manage scalability in this case. 2 options: Whenever I want to SELECT from the table, I need to include where expiry_date > NOW. If the table Post grows like a monster, I will be in trouble. Imagine after 3 years or more. Indexes will be huge ...

Messages between Users

I have body:text content:text sender_id:integer receiver_id:integer in message table I have this in user model: has_many :sent_messages, :class_name => "Message", :foreign_key => "sender_id" has_many :received_messages, :class_name => "Message", :foreign_key => "receiver_id" I have this in message model: belongs_to :sender...

Is there a print_r or var_dump equivalent in Ruby / Ruby on Rails?

I'm looking for a way to dump the structure of an object, similar to the PHP functions print_r and var_dump for debugging reasons. ...

Defining foreign key relationships for Rails' models

I have a Comment class with a :foreign_key of post_id in the Post class. class Comment < ActiveRecord::Base belongs_to :post, :class_name => "Post", :foreign_key => "post_id", :counter_cache => true belongs_to :author, :class_name => "User", :foreign_key => "author_id" end But my CreateComments migration does not define a database...

Rails page caching with Apache and capistrano

The following post explains about the page caching in rails with Nginx. http://blog.hasmanythrough.com/2008/1/30/segregated-page-cache-storage I like to implement this solution with my app but on Apache. So, the main block/route is if (-f $request_filename) { break; } if (-f /cache$request_filename) { rewrite (.*...

in Ruby on Rails, what is kENSURE and kEND in error message?

In Ruby on Rails, I sometimes get an error on the page as: compile error /Users/jian/ror/shov2/app/views/stories/index.html.erb:13: syntax error, unexpected kENSURE, expecting ')' /Users/jian/ror/shov2/app/views/stories/index.html.erb:15: syntax error, unexpected kEND, expecting ')' the kEND, i can guess that it is End....

why does this comment create a compile error on Ruby on Rails?

there is a line displayed within <pre> <%= h @stories.inspect %> and the output was too long, so i changed it to <%= #h @stories.inspect %> <% @stories.each do |s| %> <%= h s.inspect %> <% end %> (commenting out the first line). now the code will fail to compile... saying compile error /Users/winterheat/ror/shov2/app/views/sto...

Ruby on rails model and controllers inside of different namespaces

OK. This is insane. I'm new to RoR and I really want to get into it as everything about it that I have seen so far makes it more appealing to the type of work that I do. However, I can't seem to accomplish a very simple thing with RoR. I want these controlers: /admin/blog/entries (index/show/edit/delete) /admin/blog/categories (index...

Rails Voting Site

I'm getting started building a site with Rails and I would like people to be able to vote certain things with the options of "yes" or "no" or "right" or "wrong". I would also like there to be a running tally computed by percent (maybe below). Could someone tell me how I can add this functionality? Thanks ...

Java applet communication with Rails application

Hi! I'm creating a Rails application and on it, there should be a Java Applet. My question and problem is that the applet must be tightly integrated with the Rails parts. I must be able to get a list of all users, update an image, etc... And there's a surprisingly small amount of information available on the Internet of how to use appl...

on Rails, is there any way to output content besides the <%= %> tag?

it seems that on Rails or erb, the only way to output anything is by <%= %> <% puts "hello" %> won't work, unlike PHP. is there any other method at all? ...

on Rails, is f.submit in a form_for newer than submit_tag?

some books or even the rails api uses form_for ... ... submit_tag ... end and i found that the Rails 2.3.2 Scaffold uses f.submit "Create" instead... and this is not in the rails api doc. Is this a new addition and is it suppose to replace submit_tag? ...