Linking a model to a specific user - RESTful Authentication

Hi,

I am having a really tough time figuring this out. I followed the
tutorial below to add a RESTful authentication to a Ruby application
that tracks projects (just a title and a url). The tutorial is for a
blog, but I just changed blog to projects

http://ruby.about.com/od/rubyonrails/ss/railsblog3.htm

My main table of projects is:

projects

Make a field in your project model called user_id. Then in the
Project.rb file put

belongs_to :user

And in the User model

has_many :projects

Then when you create a project, set user_id = current_user.id. Finally
to display them simply use user.projects

Hope this helped.

Hello Mark, thank you for your suggestion. I gather that the RESTful
ACL plugin is meant to be used instead of RESTful Authentication Plugin
that I am currently using?

Vince

Mark Mr wrote:

Cisco Ri wrote:

Ar Chron wrote:

Umm... no.

Table 'users' already has an implicit 'id' field (you don't have to
mention them in your migrations), just like your 'links' table does. If
you are sticking to the rails standard, you needn't declare them, they
are the rails default primary key for their respective tables.

In your 'links' table, a 'user_id' field tells rails that:

a) this field, 'user_id', contains an id to a record in another table -
i.e., this record "belongs to" that record in that table over there,

and that

b) the related table is 'users' (field name - '_id', pluralized).

Thanks for letting me know. In links/new, how would I go about
including the current user_id? The only way I know of would be a hidden
form element, and I would like to keep it all server side.

Hi Cisco,

I will post my solution later tonight. I'm at work so I don't have
access.

Cisco Ri wrote:

Thanks for letting me know. In links/new, how would I go about
including the current user_id? The only way I know of would be a hidden
form element, and I would like to keep it all server side.

Restful Authentication has the notion of current_user, does it not?
Check in the authenticated_system.rb file in lib for a peek at some
methods you have available to you.

There's no need to know the current_user's id in the links controller's
new action, that just needs to manufacture an @link for the new.html.erb
form.

You will need it for the create action in the links controller though.
Something like:

def create
  @link = Link.new(params[:link])
  if logged_in?
    @link.user_id = current_user.id
    if @link.save
      redirect_to(@link)
    else
      render :action => 'new'
    end
  else
    # redirect to your login page?
    # I'd actually defend this method with a before_filter, and get
    # rid of this if logged_in? stuff
  end
end

Ar Chron wrote:

There's no need to know the current_user's id in the links controller's
new action, that just needs to manufacture an @link for the new.html.erb
form.

You will need it for the create action in the links controller though.
Something like:

You can do it a little more simply with the "build" helper, provided
there's a has/belongs_to relationship between the user and the link.

@link = current_user.links.build(params[:link])

...will automatically set the user_id field by virtue of the association.

- Aaron

Cisco Ri wrote:

Thanks a bunch everyone, it's working beautifully.

One more question: how would I submit a new link using a GET request?