Community request - can someone show me REST?

I mentioned this in another thread, but I’ve got a formal request now.
After reading tons of stuff about REST, I don’t really get it. I
need to see an example.

I’d like someone to write up an example blog app with these requirements

  • RESTful using the simply_restful plugin (or edge rails)
  • allows posting of comments to articles
  • has categories for posts

No need for authentication.

I have two questions that would be answered by that example app. The
first is just how to use simply_restful in the first place. I also
wonder how you’re supposed to use REST when the only thing interesting
about a particular resource is its associations. i.e. does
http://myapp/category/1 just display a list of posts in that category?

This may go unnoticed, I’m not sure. I already tried to start this on
my own so I could figure it out…but I got stuck very early on. I’d
find that example app very illuminating. If someone with the knowhow
could take the time to do this, I’d be very appreciate, as would many
others I’m sure.

Pat

Pat M. <[email protected]…> writes:

I’d like someone to write up an example blog app with these requirements

  • RESTful using the simply_restful plugin (or edge rails)
  • allows posting of comments to articles
  • has categories for posts

Here you go:

http://randomoracle.com/stuff/RestBlog.tar.gz

(It uses tags instead of categories. Categories are so Web 1.0!)

You’ll have to untar it, then:

  • rake rails:freeze:edge
  • rake migrate
  • script/server

and it should just work (assuming you have sqlite3 installed). No tests,
sorry.
If anything doesn’t work, or you want an explanation, just ask (but CC
me,
as I don’t read this group).

I also wonder how you’re supposed to use REST when the only thing interesting
about a particular resource is its associations. i.e. does
http://myapp/category/1 just display a list of posts in that category?

In this case, /tags/1 will do just that. But you can also go to /tags to
get a
list of all tags, or destroy tag 1 by sending DELETE to /tags/1. In a
different implementation, you might also have /tags/new, or
/tags;search.

Regards,
Alisdair McDiarmid
TrackChair
http://www.trackchair.info/

On 8/1/06, Alisdair McDiarmid [email protected] wrote:

Pat M. <[email protected]…> writes:

I’d like someone to write up an example blog app with these requirements

  • RESTful using the simply_restful plugin (or edge rails)
  • allows posting of comments to articles
  • has categories for posts

Here you go:

http://randomoracle.com/stuff/RestBlog.tar.gz

Hi Alisdair,

I found this VERY informative, so thanks for doing it. The only
question I have is regarding the nested resources.

map.resources :articles do |article|
article.resources :comments
end

I don’t understand why you need to do that. I also don’t understand
why it needs to be done in that manner, rather than simply doing
map.resources :comments.

Pat

On 8/2/06, Pat M. [email protected] wrote:

Here you go:
end

I have another question regarding this one. How does link_to and
form_for
work with the REST principles? Is there anything special that you need
to
do or is it all done behind the scenes by the routes?

Pat,
You need to do this to allow simply_restful to build the correct
url. using the example given, comments are only relevant when tied to
an article.

So a url like:
comments/3 doesn’t make any sense
You need nesting to get
articles/3/comments/

Hope this helps,
Zack

So a url like:
comments/3 doesn’t make any sense
You need nesting to get
articles/3/comments/

/comments/3 makes sense. But this nesting builds in a certain layer
of security. Instead of Comment.find(params[:id]) you do:

@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])

This way you can never grab a comment in another post, for example.

link_to and form_for takes the :method option.

Example:

	<%= link_to 'Delete', { :action => 'destroy', :method => :delete },
														:method => :delete,
														:confirm => 'Are you sure? There is NO undo!' %>

form_for is similar. See the documentation for more details.

Hope this helps,
Zack

On 8/1/06, Rick O. [email protected] wrote:

This way you can never grab a comment in another post, for example.

Where is this post_id coming from?

Basically I’m wondering now that we have
map.resources :articles do |article|
article.resources :comments
end

When/where would I actually use it?

Pat

Daniel ----- wrote:

On 8/2/06, Pat M. [email protected] wrote:

Here you go:
end

I have another question regarding this one. How does link_to and
form_for
work with the REST principles? Is there anything special that you need
to
do or is it all done behind the scenes by the routes?

When you have:

map.resource :widgets

in your routes.rb, there are a bunch of named urls created for you to
use.

widgets_url => /widgets (previously /widgets/list)
widget_url(@widget) => /widgets/1
new_widget_url => /widgets/new
edit_widget_url(@widget) => /widgets/1;edit

Pat,

You use the routes that simply_restful creates. link_to, form_for,
url_for, etc. use the routes created by map.resources. SR also
creates named routes. Use Rick’s excellent routing navigator to see
them all:

http://weblog.techno-weenie.net/2006/6/19/new-plugin-routing-navigator

Zack

On 8/2/06, Rick O. [email protected] wrote:

@comment = @post.comments.find(params[:id])
Hi Rick,

thanks for the insights!!

IMHO it makes sense to use urls like /articles/3/comments in the views
as
well. But I don’t get the railish way of constructing something like
/articles/3/comments/new or /articles/3/comments/1;edit with link_to
yet.

As I wrote in another post something like <%= link_to ‘Add Comment’,
:controller => ‘articles’, :action => “#{article_id}/comments/new” %>
does
what I want from a technical perspective, but results in an encoded url
(
http://localhost:3002/articles/1%2Fcomments%2Fnewhttp://localhost:3002/articles/1/comments/new)
which doesn’t look as elegant as the whole approach… There’s got to
be a
railish way to construct this url unencoded. I just don’t get it.

I would love the readibility of urls like this and as you wrote they
introduce a level of security as well.

Cheers,
Jan

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs