Nested ressource path problem

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 → /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

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

But it didn’t help me out :frowning:

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 :slight_smile:

On Sat, Jul 19, 2008 at 8:40 AM, Parity [email protected]

Hi :slight_smile:

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

: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 [email protected]
wrote:

Hi :slight_smile:

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

Okay, I post you as much information as i can :slight_smile:

This is my db layout:
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 :slight_smile:

Patrick

On 19 Jul., 11:28, “bala kishore pulicherla” [email protected]

un comment the map.resources :comments :slight_smile:

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 [email protected]

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

One Final try :slight_smile:

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 :slight_smile: :slight_smile:

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