帖子中的NoMethodError#show undefined method`post_comments_path'

时间:2014-03-20 10:29:15

标签: ruby-on-rails-4

I used associations in the models whenever I am applying associations I am getting problem ,I am new to ruby as well as rails.I am using Rails4,Eclipse,Windows Xp(sp3) and mysql5.6 ,I am getting the above error when I want clicked the show link the error is 

undefined method `post_comments_path' for #<#<Class:0x2aef820>:0x2bd7df8>

Extracted source (around line #25):

<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>...here it is line number 25
<p>
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>

I have two model
post.rb
class Post < ActiveRecord::Base
 has_many :comments
  validates :title, presence: true,
                    length: { minimum: 5 }
end

comment.rb
class Comment < ActiveRecord::Base
  belongs_to :post
end
I have two controllers
posts_controller.rb
class PostsController < ApplicationController
  def new
    @post=Post.new
end
def show
  @post = Post.find(params[:id])
end
def edit
  @post = Post.find(params[:id])
end
def update
  @post = Post.find(params[:id])

  if @post.update(params[:post].permit(:title, :text))
    redirect_to @post
  else
    render 'edit'
  end
  end
  def destroy
  @post = Post.find(params[:id])
  @post.destroy
 redirect_to posts_path
end
  def create
  @post = Post.new(post_params)
  # it will also works @post=Post.new(params[:post].permit(:title,:text))
 if @post.save
  redirect_to @post
  # or this command also works redirect_to action: :show, id: @post.id
else
  render 'new'
end
end

def index
  @posts = Post.all
end
private
  def post_params
    params.require(:post).permit(:title, :text)
  end

end

comments_controller.rb

class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(params[:comment].permit(:commenter, :body))
    redirect_to post_path(@post)
  end
end
my routes.rb file is
Blog::Application.routes.draw do
  resources :posts do
  resources :comments
end

I have two migration files 
create_posts.rb
class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :title
      t.text :text

      t.timestamps
    end
  end
end

create_comments.rb

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.string :commenter
      t.text :body
      t.references :post

      #above line sets foreign key column for the association between the two models.

      t.timestamps
    end
        #and bellow add_index line sets up an index for this association column.
  add_index :comments, :post_id
  end
end
my show.html.erb file is
<p>
  <strong>Title:</strong>
  <%= @post.title %>
</p>

<p>
  <strong>Text:</strong>
  <%= @post.text %>
</p>

<h2>Comments</h2>
<% @post.comments.each do |comment| %>
  <p>
    <strong>Commenter:</strong>
    <%= comment.commenter %>
  </p>

  <p>
    <strong>Comment:</strong>
    <%= comment.body %>
  </p>
<% end %>

<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
  <p>
    <%= f.label :commenter %><br />
    <%= f.text_field :commenter %>
  </p>
  <p>
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>

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

我在模型中使用了关联,每当我应用关联时我都会遇到问题,我是ruby以及rails的新手。我正在使用Rails4,Eclipse,Windows Xp(sp3)和mysql5.6,我得到了上面的错误,当我想点击显示链接错误是我在模型中使用关联,每当我应用关联我遇到问题,我是新的ruby以及rails.I我使用Rails4,Eclipse,Windows Xp(sp3)和mysql5.6,当我想点击显示链接错误是

时,我收到上述错误

1 个答案:

答案 0 :(得分:0)

我觉得你错过了有关嵌套资源的部分。如:

resources :posts do
  resources :comments
end

这意味着资源生成的路线:评论嵌套在您的帖子中,这会产生如下网址:

/posts/1/comments