One-to-One Associations and REST

Hello,

I’m trying to write a piece of code that enables users to upload an
avatar. The user code and the avatar code does work great, but now I
want to associate the avatar with the user, which should also be
reflected in the urls.

I’ve read the B-Simple REST tutorial (http://www.b-simple.de/documents)
which explains this process for one-to-many associations. But things are
a little different for me, because I only want one avatar per user, I do
not need an index action and the urls do look different. For example, I
want /user/:user_id/avatar to show the user avatar,
/user/:user_id/avatar;edit to change the avatar etc.

Is there any way to achieve this?

Hi,
I’m still finding my way around RoR myself but isn’t what you want
something
like?:

class User < ActiveRecord::Base
has_one :Avatar, :foreign_key => “your_avatar_id_key”,
end

class Avatar < ActiveRecord::Base
belongs_to :User, :foreign_key => “you_user_id_key”,
end


Regards

Andrew

Hello Andrew,

thanks for your reply. Unfortunately this does not help me, because this
is for the associations only. I need help for reflecting these
associations in my REST enabled urls.

Andrew M. wrote:

Hi,
I’m still finding my way around RoR myself but isn’t what you want
something
like?:

class User < ActiveRecord::Base
has_one :Avatar, :foreign_key => “your_avatar_id_key”,
end

class Avatar < ActiveRecord::Base
belongs_to :User, :foreign_key => “you_user_id_key”,
end


Regards

Andrew

I’ve read the B-Simple REST tutorial (http://www.b-simple.de/documents)
which explains this process for one-to-many associations. But things are
a little different for me, because I only want one avatar per user, I do
not need an index action and the urls do look different. For example, I
want /user/:user_id/avatar to show the user avatar,
/user/:user_id/avatar;edit to change the avatar etc.

map.resources :people do |people|
people.resource :avatar
end

avatar_url(:person_id => person) # => /people/5/avatar
edit_avatar_url(:person_id => person) # => /people/5/avatar;edit

on a similar note, what’s generally regarded as best practice for a
REST-ful multi-lingual site, let’s say one which uses Globalize,
where the language code has to be passed around in the URL (so that
pages can be cached easily per language)?

If the first part of the URL always holds the language code, e.g.:

http://mysite.tld/en/
http://mysite.tld/de/
http://mysite.tld/es/

How is this declared using resource/resources? An outer map.resource
which contains all other route mappings?

3 mar 2007 kl. 17.45 skrev DHH:

DHH wrote:

map.resources :people do |people|
people.resource :avatar
end

avatar_url(:person_id => person) # => /people/5/avatar
edit_avatar_url(:person_id => person) # => /people/5/avatar;edit

Great, thanks! I guess I should not have missed this blogpost:

One last thing to mention: The avatar controller has to be singular,
too. This means the controller name (AvatarController) and the file name
(avatar_controller.rb). I missed the file name first, which took me
quite some time to fiddle out…