开发者

has_many, belongs_to and comment forms

开发者 https://www.devze.com 2022-12-27 17:19 出处:网络
I\'m having two models: Snippet and SnippetComment. These also have their own controllers, as well as views. In /app/views/snippets/show.html.erb I have a form that starts like this:

I'm having two models: Snippet and SnippetComment. These also have their own controllers, as well as views. In /app/views/snippets/show.html.erb I have a form that starts like this:

<% form_for(@new_snippet_comment) do |form| %>

SnippetComment belongs to one Snippet, and a Snippet belongs to one User. This means that I have this routing (making it non-RESTful == making it suck, as you know):

map.resources :users do |user|
  user.resources :snippets do |snippet|
    snippet.resources :snippet_comments, :as => "comments"
  end
end

So when I submit the SnippetComment form in the SnippetController#show view, this request should be made:

POST /users/x/snippets/x/comments HTTP/1.1

(where x is the User's or Snippet's id).

The problem is that I don't even get the comment submission form, but rahter this:

NoMethodError in Snippets#show

Showing app/views/snippets/show.html.erb where line #29 raised:

undefined method `snippet_comments_path' for

Extracted source (around line #29):

26: <% if current_user %>
27: <h2>Schrijf een nieuwe reactie</h2>
28: 
29: <% form_for(@new_snippet_comment) do |form| %>
30: 
31: <p>
32:   <%= form.text_area :body %>

RAILS_ROOT: /Users/jeffatwood/Dev/youjustdontneedmyrealname

Application Trace | Framework Trace | Full Trace

/Users/jeffatwood/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/polymorphic_routes.rb:107:in `__send__'
/Users/jeffatwood/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/polymorphic_routes.rb:107:in `polymorphic_url'
/Users/jeffatwood/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/polymorphic_routes.rb:114:in `polymorphic_path'
/Users/jeffatwood/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:298:in `apply_form_for_options!'
/Users/jeffatwood/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:277:in `form_for'
/Users/jeffatwood/Dev/youjustdontneedmyrealname/app/开发者_如何学运维views/snippets/show.html.erb:29:in `_run_erb_app47views47snippets47show46html46erb'
/Users/jeffatwood/Dev/youjustdontneedmyrealname/app/controllers/snippets_controller.rb:19:in `show'

Request

Parameters:

{"id"=>"1",
 "user_id"=>"2"}

Show session dump

Response

Headers:

{"Content-Type"=>"text/html",
 "Cache-Control"=>"no-cache"}

Oh, in snippets_controller.rb I have this:

def show
  @snippet = Snippet.find(params[:id], :conditions => { :user_id => params[:user_id] })
  @new_snippet_comment = SnippetComment.new

  respond_to do |format|
    format.html
    format.xml
    format.json
  end
end

Can anyone help me with this problem? Thanks


You should pass snipped_id when you're creating @new_snippet_comment


You have two levels of nesting ( i.e. user -> snippet -> comment) so your form_for syntax should be as follows:

form_for [@user, @snippet, @comment]
0

精彩评论

暂无评论...
验证码 换一张
取 消