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.
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
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
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.:
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…
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.