3.10.12

Route by name, Not by id.

Environment:

I'm using Ubuntu 12.04 64bit with RubyMine 4.5.4Ruby 1.9.3Rails 3.2.8.

Problem:

It's more a feature than a problem :P
I wanted to access my model by name and no by id for example:

By id:         |     By name:  
/friend/1     |   friend/john

 

Research:

By default controllers use the following code to get the required instance from the database:

@friend = Friend.find(params[:id])
respond_to do |format|
    format.html # show.html.erb
end

so first thing to try is:
@friend = Friend.find_by_name(params[:name])

ERROR!

So next i found map.connect...

map.connect 'friend/:name', :controller => 'friends', :action => 'show'
I found out that map.connect only available in rails 3.2 and was deprecated in rails 3.x 

 

Solution:

Have you ever heard of to_param ?
I just needed to add the following to my model (friend.rb):


def to_param
   name
end
And in the controller (friend_controller.rb):

@friend = Friend.find_by_name(params[:id]) 

No need to change anything in routes.rb!


ActiveAdmin fix:

For those of us who uses ActiveAdmin gem like me, there is another fix to be able to edit friends by name.
Add the following to your friends.rb: (ActiveAdmin.register Friend do)


before_filter :only => [:show, :edit, :update, :destroy] do
  @friend = Friend.find_by_name(params[:id])
end

7.8.12

Ruby on Rails GemNotFound / FetchError / connection attempt failed!

I didn't post for a very long time!
I had alot of sketches though but didn't had the time to finish and publish them..

But i hope to come back now with more posts!
Also i'm gonna stick to a new format in my posts thati think is better to read. maybe i'll also update old posts to this format.

I just started learning Ruby on Rails few weeks ago and ran into a very weird problem.

Environment:


I'm using Windows 7 64bit with RubyMine 4.5.1, Ruby 1.9.3, Rails 3.2.

I was able to install all this working environment on my Desktop but not on my Laptop..


Problem:


I encounterd the following errors on cmd & RubyMine:
C:\Users\Danpe>gem install rails
WARNING:  Error fetching data: Errno::ECONNREFUSED: No connection could be made
because the target machine actively refused it. - connect(2) (http://rubygems.org/latest_specs.4.8.gz) 
`block in materialize': Could not find rake-0.9.2.2 in any of the sources (Bundler::GemNotFound)

Fetching source index for `http://rubygems.org/`  Could not reach rubygems repository `http://rubygems.org/`  Could not find gem 'rspec-rails (>= 2.0.0.beta.22, runtime)' in any of the gem sources.

 ERROR: http://rubygems.org/ does not appear to be a repository       ERROR:  While executing gem ... (Gem::RemoteFetcher::FetchError)
 Errno::ETIMEDOUT: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed
because connected host has failed to respond. - connect(2)           (http://rubygems.org/yaml)
 I got all these kind of weird errors!

Research:


 so i started looking around the web and around my PC network settings.

I double checked my Internet Options to see that there is no proxy enabled, and there was none!
I opened cmd and typed SET to see my Environment Variables:

C:\Users\Danpe>SET
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\Danpe\AppData\Roaming
COMPUTERNAME=DANPE-ZENBOOK
ComSpec=C:\Windows\system32\cmd.exe
HOMEDRIVE=C:
HOMEPATH=\Users\Danpe
HTTP_PROXY=192.168.0.1:8080
 As you can see there is an Environment Variable for HTTP_PROXY...

Solution:


Go to Start (WinKey) type Edit the system environment variables and you'll get this window:


Click on Environment Variables and then remove HTTP_PROXY from System Variables!

Make sure you reopen cmd and RubyMine :)