Pertanyaan Tidak ada kesalahan metode dalam aplikasi rel 3


Saya mendapatkan kesalahan ini:

undefined method `post_comments_path' for #<#<Class:0x1052a6e98>:0x1052a4be8>
Extracted source (around line #27):

24: 
25: <% end%>
26: 
27: <% form_for [@post, Comment.new] do |f| %>
28:   <p>
29: 
30:     <%= f.label :name, "Author" %><br />

Rute saya:

Myblog::Application.routes.draw do

  root      :to => 'posts#index'

  resources :comments

  resources :posts, :has_many => :comments

post.rb

class Post < ActiveRecord::Base
  has_many :comments
end

comment.rb

class Comment < ActiveRecord::Base
  belongs_to :post
end

views / posts / show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <b>Title:</b>
  <%= @post.title %>
</p>

<p>
  <b>Body:</b>
  <%= @post.body %>
</p>

<h2>Comments</h2>

<% @post.comments.each do |c|%>

    <p>
        <b><%=h c.name %>said: </b>
        <%= time_ago_in_words(c.created_at)%> ago       
    </p>
    <p>
        <%= c.body%>
    </p>

<% end%>

<% form_for [@post, Comment.new] do |f| %>
  <p>

    <%= f.label :name, "Author" %><br />
    <%= f.text_field :name %><br />
    <%= f.label :body, "Comment Description" %><br />
    <%= f.text_area :body %>
  </p>

  <p>
    <%= f.submit "Add Comment" %>
  </p>
<% end %>


<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>

Bingung karena saya tidak melihat referensi ke post_comments_path ??

rute rake:

root        /(.:format)                  {:action=>"index", :controller=>"posts"}
    comments GET    /comments(.:format)          {:action=>"index", :controller=>"comments"}
    comments POST   /comments(.:format)          {:action=>"create", :controller=>"comments"}
 new_comment GET    /comments/new(.:format)      {:action=>"new", :controller=>"comments"}
edit_comment GET    /comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
     comment GET    /comments/:id(.:format)      {:action=>"show", :controller=>"comments"}
     comment PUT    /comments/:id(.:format)      {:action=>"update", :controller=>"comments"}
     comment DELETE /comments/:id(.:format)      {:action=>"destroy", :controller=>"comments"}
       posts GET    /posts(.:format)             {:action=>"index", :controller=>"posts"}
       posts POST   /posts(.:format)             {:action=>"create", :controller=>"posts"}
    new_post GET    /posts/new(.:format)         {:action=>"new", :controller=>"posts"}
   edit_post GET    /posts/:id/edit(.:format)    {:action=>"edit", :controller=>"posts"}
        post GET    /posts/:id(.:format)         {:action=>"show", :controller=>"posts"}
        post PUT    /posts/:id(.:format)         {:action=>"update", :controller=>"posts"}
        post DELETE /posts/:id(.:format)         {:action=>"destroy", :controller=>"posts"}

5
2017-09-23 02:01


asal


Jawaban:


Anda tidak memiliki post_comments_path yang ditentukan melalui rute Anda. Anda tidak seharusnya menggunakannya :has_many di rute (hanya model), tetapi buat mereka sebagai gantinya. Ini harus melakukan trik:

resources :posts do
  resources :comments
end

10
2017-09-23 02:12



Saya pikir masalahnya ada di rute Anda.

resources :posts, :has_many => :comments

Lihat blog ini, mereka mengubah rute cukup banyak: http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/

Di Rails 2, sumber daya bertingkat berada   ditentukan oleh blok atau dengan menggunakan   : has_many atau: has_one key. Keduanya   ini telah digantikan oleh blok,   memberi mereka antarmuka yang lebih Rubyish   untuk menentukan sumber daya terkait.

Coba ini:

resources :posts do
  resources :comments
end

3
2017-09-23 02:15