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
Alisdair McDiarmid
2006-Aug-01 15:35 UTC
[Rails] Re: Community request - can someone show me REST?
Pat Maddox <pergesu@...> 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 postsHere 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/
Pat Maddox
2006-Aug-02 00:31 UTC
[Rails] Re: Community request - can someone show me REST?
On 8/1/06, Alisdair McDiarmid <alisdair@randomoracle.org> wrote:> Pat Maddox <pergesu@...> 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.gzHi 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 Maddox <pergesu@gmail.com> wrote:> > On 8/1/06, Alisdair McDiarmid <alisdair@randomoracle.org> wrote: > > Pat Maddox <pergesu@...> 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >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? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060802/37f46ab7/attachment.html
Zack Chandler
2006-Aug-02 00:51 UTC
[Rails] Re: Community request - can someone show me REST?
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, Daniel N <has.sox@gmail.com> wrote:> > > On 8/2/06, Pat Maddox <pergesu@gmail.com> wrote: > > On 8/1/06, Alisdair McDiarmid <alisdair@randomoracle.org> wrote: > > > Pat Maddox <pergesu@...> 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 > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > 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? > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Zack Chandler
2006-Aug-02 00:53 UTC
[Rails] Re: Community request - can someone show me REST?
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 On 8/1/06, Pat Maddox <pergesu@gmail.com> wrote:> On 8/1/06, Alisdair McDiarmid <alisdair@randomoracle.org> wrote: > > Pat Maddox <pergesu@...> 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Rick Olson
2006-Aug-02 01:38 UTC
[Rails] Re: Community request - can someone show me REST?
> 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. -- Rick Olson http://techno-weenie.net
Pat Maddox
2006-Aug-02 01:53 UTC
[Rails] Re: Community request - can someone show me REST?
On 8/1/06, Rick Olson <technoweenie@gmail.com> wrote:> > 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.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
Zack Chandler
2006-Aug-02 05:36 UTC
[Rails] Re: Community request - can someone show me REST?
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/1/06, Pat Maddox <pergesu@gmail.com> wrote:> On 8/1/06, Rick Olson <technoweenie@gmail.com> wrote: > > > 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. > > 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 8/2/06, Rick Olson <technoweenie@gmail.com> wrote:> > > 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])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%2Fnew<http://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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060802/6ba09af6/attachment.html
Kevin Williams
2006-Aug-02 14:38 UTC
[Rails] Re: Re: Community request - can someone show me REST?
Daniel ----- wrote:> On 8/2/06, Pat Maddox <pergesu@gmail.com> 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 -- Posted via http://www.ruby-forum.com/.
Pat Maddox
2006-Aug-02 16:25 UTC
[Rails] Re: Community request - can someone show me REST?
On 8/2/06, Alisdair McDiarmid <alisdair@randomoracle.org> wrote:> On 8/2/06, Pat Maddox <pergesu@gmail.com> wrote: > > 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. > > By the way, the routing automatically creates the :article_id parameter > based on the class name of the enclosing route, which is a bit magical > (but then this is Rails, so you should be used to that :-)To clarify, do you just mean that comment_url(1, 2) will result in /articles/1/comments/2 and set params[:article_id] for you? Also, for anyone else that''s having some trouble with this, I''ve found that playing with the routes in console is very illuminating. Load up console and do app.articles_url app.article_url(article) app.comment_url(article, comment) of course you have to have created an article and comment object first to pass in. After messing with that for a bit though, the automatic routes made a lot more sense to me. Pat
Alisdair McDiarmid
2006-Aug-02 17:18 UTC
[Rails] Re: Community request - can someone show me REST?
On 8/2/06, Pat Maddox <pergesu@gmail.com> wrote:> On 8/2/06, Alisdair McDiarmid <alisdair@randomoracle.org> wrote: > > By the way, the routing automatically creates the :article_id parameter > > based on the class name of the enclosing route, which is a bit magical > > (but then this is Rails, so you should be used to that :-) > > To clarify, do you just mean that comment_url(1, 2) will result in > /articles/1/comments/2 and set params[:article_id] for you?Yes, exactly that, and also set params[:id] (to 2). But also, comment_url(@article, @article.comments.first) will do something similar. How your model converts to route parameters is up to you: the default is the primary key, but you can override to_param if you want, and return a different (unique) value. This is useful if you don''t want ids in URLs. Regards, Alisdair
Possibly Parallel Threads
- Resotolog-1.2 - RESTful blog example (was Community request - can someone show me REST? )
- So what is REST? I don''t get how it fits in Rails
- Edge RSpec on Rails...what did I forget?
- What''s the new syntax for predicates?
- it "should [action] ..." vs it with an active voice