ruby-on-rails

'sudo gem install' or 'gem install' and gem locations

running 'sudo gem list --local' and 'gem list --local' gives me differing results, my gem path is set to my home folder and only contain the gems from 'gem list --local'. So it's probably not good to have gems installed in different directories on my computer, should I have the gem path set differently? and should I always use sudo when...

Rails optional gem config

What do you do when you want to use a gem for development/testing that you don't want to force other devs to use? Right now I have begin require 'redgreen' rescue LoadError end in test_helper.rb and no gem config, but that seems like a clumsy approach, albeit a functional one. I'd like to do something like the following: config....

subdomain routing

Hi all, When a user types in his username.example.com I read out the subdomain and render the users personal page. map.home '', :controller => 'users', :action => 'show' the problem is that I now want to add custom subdomains like www, '', help and so on. What I do now but doesn't work: I read the subdomain in the applications con...

bypass attr_accessible/protected in rails

I have a model that, when it instantiates an object, also creates another object with the same user id. class Foo > ActiveRecord::Base after_create: create_bar private def create_bar Bar.create(:user_id => user_id #and other attributes) end end In Bar.rb I have attr_protected to protect it from hackers. class Bar > ActiveRecord...

Rails – select current menu item

I have made a small helper that adds class="selected". Primarily it uses current_page? to investigate if the present path is the current menu item, and the select it. module MenuHelper #renders menu items and emphasizes current menu item def topmenu pages = { "products" => admin_products_path, "categories" => admin_c...

Worth learning Ruby if not using Rails?

Hello! So I took a quick look at Ruby some days ago. At first I was quite astonished by some things it features which really seem to make a programmers life easier. (Use of blocks, object model,...) But then there is this big problem: Nobody seems to be using Ruby without Rails! The #Ruby(-lang) channel is dead, people are only talking...

rails belongs_to has_one. need some explanation

I've got two models: Customer and Contact Customers table has columns :id, :firstName, :lastName Contacts table has columns :id, :cid, :hphone, :cphone So if Customers table has data 1 Josh McDonnel Then Contacts table has corresponding 5 1 947-245-2342 342-543-8585 What associations can I use here? Will Contact have...

When and where do I require files in a rails application?

Let's say I have I have the following file in my lib directory of my rails application: #lib/proxy.rb module SomeService class ServiceProxy def do_something end end end If I want to use ServiceProxy in a model, I can use it like so: #app/models/product.rb require 'proxy' class Product < ActiveRecord::Base def do_someth...

loaded? always returns true for belongs_to associations?

I have some geography related classed defined as follows: class Location < ActiveRecord::Base end class State < Location has_many :geographies has_many :cities has_many :counties end class County < Location belongs_to :state has_many :geographies has_many :cities, :through => :geographies, :uniq => true end class City...

How can I access the Rails logger from an initializer?

Following the advice from my previous question, I placed my background process in an initializer named scheduler.rb. However, I'm having a hard time getting the newly-scheduled processes to log to the Rails logs. Is there a simple way for me to access the same logs from the initializer, preferably by accessing Rails' default logger metho...

Rails data modeling - alternatves to has_many :through, given polymorphism?

I'm working on an application that allows users to associate images with specific events. Events are owned by a user. The easy solution would of course be: class Image < ActiveRecord::Base belongs_to :event end class Event < ActiveRecord::Base has_many :images belongs_to :user end class User < ActiveRecord::Base has_many :eve...

Always use responds_to?

Until now I have always specified the format of the response for actions using a responds_to block, like so: responds_to do |format| format.js { render :json => @record } end Recently I realized that if you only support one format (as in the above example), you don't really need that block. Is it best practice to leave it in, or re...

Creating Link with Ruby on Rails

Obviously, I am new to this. I need to make a link using rails to something within the model. The code I have is this. It is probably not even the correct syntax: <li id="nav_home"><%= link_to 'SOMETHING', {:controller => 'inventories', :action => 'home'} %></li> This code defaults to creating a text link, but I want the link elemen...

SSL with Ruby on Rails

What do I need to do to get traffic to my ruby on rails app to use https? I have a certificate installed and if I manually type in "https://" in the address bar when accessing the site the little lock icon appears, but just manually going to www.example-app.com in my browser sends traffic through http://. Is there some one-line config...

Inject an @ variable into src of an IFRAME

I have a dynamic url that I'm building in my controller to serve as the src of the IFRAME. I'm currently saving the url as @iframe_url. When I go to the IFRAME on the view I'm doing <IFRAME src=<%=@iframe_url%> /> to no avail. Can anyone help me out? ...

Using increment(attribute) to set value for user's rating

I'm thinking about writing an app with question and answer model. Answer has increment attribute that anyone can change to change the rating value. So if I click up on answer that someone else posted, the answer will show 1 point. But, how do I/can I, use that to increase the rating of the overall user. In essence it is very similar to ...

Dealing with high number of real-time calls to partner API in Rails

My Rails app needs to make a call to a partner's RESTful API for every request that it receives on a particular controller's action. I need to pass in request-specific parameters (e.g. IP, user-agent, etc.) to the partner API, and return the response I get to the user. Since the call to the partner API is very user-specific, I can't cach...

How do you use an instance variable with mailer in Ruby on Rails?

I created an instance variable (@user) in the model of a mailer and want to access it in the view? But it's giving me an an error (@user = nil). What's the best way to pass a variable to view (the email body)? Thanks, Chirag ...

Help: Adding rating to existing posts model in Rails

I've a simple posts model with title:string and rating:integer and I want to add the ability to rate the posts. So far I have #Post controller def increase @post = Post.find(params[:id]) @post.increment! :rating flash[:notice] = "Thanks for your rating." redirect_to @post end #Post show <%= link_to "Rating", increase_post_path %...

How to restrict user access page in RoR?

I know that I can have a session after the user login. Also, I created a helper method called "current_user". I don't want to other people which are not signed in can get access to the page. Apart from making doing this, how can I do? I can do this to not allow people the get access to the content, but I don't want the user within login...