# Nested ressource path problem

**URL:** <https://www.ruby-forum.com/t/nested-ressource-path-problem/143484>\
**Category:** Rails\
**Created:** [July 19, 2008, 5:01am UTC](https://www.ruby-forum.com/t/nested-ressource-path-problem/143484 "2008-07-19T05:01:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Parity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@Parity](https://www.ruby-forum.com/u/Parity)\
**Post date:** [July 19, 2008, 5:01am UTC](https://www.ruby-forum.com/t/nested-ressource-path-problem/143484/1 "2008-07-19T05:01:09Z")

</div>

Hello :-),

I’ve started to figure out how nested ressources are working, but I  
get an exception in a (i think so) simple case. The exception is:  
“ActionController::MethodNotAllowed - Only get, put, and delete  
requests are allowed.”

webrick tells me:  
127.0.0.1 - - [19/Jul/2008:04:40:55 CEST] “POST /articles/22/comments/  
new HTTP/1.1” 405 7131 [http://localhost:3000/articles/22](http://localhost:3000/articles/22) → /articles/  
22/comments/new

I see its not a get, put or delete.

routes.rb:  
map.resources :comments  
map.resources :articles, :has\_many =\> :comments

articles/views/show.html.erb:  
\<%= link\_to\_remote(“new comment”,  
:update =\> ‘editor’,  
:url =\> new\_article\_comment\_path(@article),  
:complete =\> visual\_effect(:blind\_down,  
“editor”, :duration =\> 2.5)) %\>

So why didn’t I get a get request? Is it a clean code?

I read the RESTful Rails Development from b-simple, but i didn’t found  
a solution for the problem. Maybe the document is a bit outdated. If  
you have any solution for the problem or a URL I would be glad to hear  
it _g_

Thanks for reading and have a nice day,  
Patrick

---

<div class="post-metadata">

**Author:** ![Parity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@Parity](https://www.ruby-forum.com/u/Parity)\
**Post date:** [July 19, 2008, 5:11am UTC](https://www.ruby-forum.com/t/nested-ressource-path-problem/143484/2 "2008-07-19T05:11:05Z")

</div>

I also tried:  
:url =\> { :controller =\> “comments”, :action =\> “new”, :params =\>  
{ :article\_id =\> @article}}

But it didn’t help me out ☹

---

<div class="post-metadata">

**Author:** ![Parity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@Parity](https://www.ruby-forum.com/u/Parity)\
**Post date:** [July 19, 2008, 7:40am UTC](https://www.ruby-forum.com/t/nested-ressource-path-problem/143484/3 "2008-07-19T07:40:11Z")

</div>

u can do like this  
in ur routes file  
map.resources :articles do |article|  
article.resources :comments  
end

in ur view  
for article in @article  
ur - stuff —  
\<%= link\_to\_remote(“new comment”,  
:update =\> ‘editor’,  
new\_article\_comment\_path(article),  
:complete =\> visual\_effect(:blind\_down,  
“editor”, :duration =\> 2.5)) %\>  
end

i hope it helps 🙂

On Sat, Jul 19, 2008 at 8:40 AM, Parity [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Parity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@Parity](https://www.ruby-forum.com/u/Parity)\
**Post date:** [July 19, 2008, 11:18am UTC](https://www.ruby-forum.com/t/nested-ressource-path-problem/143484/4 "2008-07-19T11:18:12Z")

</div>

Hi 🙂

thans for your answer, but it didn’t helps :-(. I changed the lines in  
my lines in routes.rb to:  
map.resources :articles do |article|  
article.resources :comments  
end

(isn’t is the old convention?)

And my view to:

\<%= link\_to\_remote(“new comment”,  
:update =\> ‘editor’,  
:url =\> new\_article\_comment\_path(@article),  
:complete =\> visual\_effect(:blind\_down,  
“editor”, :duration =\> 2.5)) %\>

(without “:url =\>” in front of, i got a compile error)

But it didn’t helps.

Bye,  
Patrick

---

<div class="post-metadata">

**Author:** ![Parity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@Parity](https://www.ruby-forum.com/u/Parity)\
**Post date:** [July 19, 2008, 11:39am UTC](https://www.ruby-forum.com/t/nested-ressource-path-problem/143484/5 "2008-07-19T11:39:40Z")

</div>

:o

opzz

u can do like this  
in routes.rb  
map.resources :articles do |article|  
article.resources :comments  
end

On Sat, Jul 19, 2008 at 2:47 PM, Parity [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi 🙂
> 
> thans for your answer, but it didn’t helps :-(. I changed the lines in  
> my lines in routes.rb to:  
> map.resources :articles do |article|  
> article.resources :comments  
> end

and in ur view

> \<%= link\_to\_remote(“new comment”,  
> :update =\> ‘editor’,  
> :url =\> new\_article\_comments\_path(@
> 
> > article),  
> > :complete =\> visual\_effect(:blind\_down,  
> > “editor”, :duration =\> 2.5)) %\>

Note: the url will also depend up on the associations that u use in the  
models  
if still any issue :o post ur model and routes file

---

<div class="post-metadata">

**Author:** ![Parity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@Parity](https://www.ruby-forum.com/u/Parity)\
**Post date:** [July 20, 2008, 3:33pm UTC](https://www.ruby-forum.com/t/nested-ressource-path-problem/143484/6 "2008-07-20T15:33:35Z")

</div>

Okay, I post you as much information as i can 🙂

This is my db layout:  
[http://erdbeere.net/media/db\_layout.png](http://erdbeere.net/media/db_layout.png)

article model:  
class Article \< ActiveRecord::Base  
belongs\_to :blog  
has\_many :comments  
end

comment model:  
class Comment \< ActiveRecord::Base  
belongs\_to :article  
belongs\_to :user  
end

routes.rb:  
map.resources :users  
map.resources :subscriptions  
map.resources :blogs

# map.resources :comments

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

If there is anything i can du to find the problem just send an E-  
Mails 🙂

Patrick

On 19 Jul., 11:28, “bala kishore pulicherla” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Parity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@Parity](https://www.ruby-forum.com/u/Parity)\
**Post date:** [July 21, 2008, 9:45am UTC](https://www.ruby-forum.com/t/nested-ressource-path-problem/143484/7 "2008-07-21T09:45:08Z")

</div>

un comment the map.resources :comments 🙂

and in the view file

> \<%= link\_to\_remote(“new comment”,  
> :update =\> ‘editor’,  
> :url =\> new\_article\_comment\_path(@
> 
> > article),  
> > :complete =\> visual\_effect(:blind\_down,  
> > “editor”, :duration =\> 2.5)) %\>

On Sun, Jul 20, 2008 at 7:03 PM, Parity [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Parity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@Parity](https://www.ruby-forum.com/u/Parity)\
**Post date:** [August 11, 2008, 1:40pm UTC](https://www.ruby-forum.com/t/nested-ressource-path-problem/143484/8 "2008-08-11T13:40:08Z")

</div>

Well, it looks like a problem with js, but I haven’t figured out  
what’s wrong. currently i’ve deactevated the visual effect and  
use :url to generate the url. It’s not great, but it works.

Thanks,  
Patrick

---

<div class="post-metadata">

**Author:** ![Parity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@Parity](https://www.ruby-forum.com/u/Parity)\
**Post date:** [August 11, 2008, 1:55pm UTC](https://www.ruby-forum.com/t/nested-ressource-path-problem/143484/9 "2008-08-11T13:55:24Z")

</div>

One Final try 🙂

models/articles.rb  
has\_many :comments  
models/comments.rb  
belongs\_to :article

controllers comments  
before\_filter :find\_article  
def new  
@comment = Comment.new  
end

def create  
@comment = Comment.new(params[:comment])  
if (@article.comments \<\< @comment)  
redirect\_to article\_url(@article)  
else  
render :action =\> :new  
end  
end

private

def find\_article  
@article\_id = params[:article\_id]  
return(redirect\_to(articles\_url)) unless @article\_id  
@article = Article.find(@article\_id)  
end

views/comments/new.html.erb

\<% form\_for [@article, @comment] do |form| %\>

 Add a Comment

\<%= render :partial =\> ‘form’, :object =\> form %\>

\<%= submit\_tag "Create" %\>

\<% end %\>

And Finally ur routes file should be

ActionController::Routing::Routes.draw do |map|  
map.resources :articles do |article|  
article.resources :comments  
end

I hope it will work out 🙂 🙂

if u still face any issue :o u can ask  
regards,  
Balu
