Ruby on rails 4 帖子中的命名错误#显示未定义的方法"帖子注释"路径'; 每当我应用关联时,我都会在模型中使用关联。我遇到问题,我对ruby和rails都是新手。我使用Rails4、Eclipse、Windows Xp(sp3)和mysql5.6,当我想要单击“显示”链接时,会出现上述错误 未定义的方法“post\u comments\u path”# 提取的源(第25行附近): 添加评论: …这是25号线

Ruby on rails 4 帖子中的命名错误#显示未定义的方法"帖子注释"路径'; 每当我应用关联时,我都会在模型中使用关联。我遇到问题,我对ruby和rails都是新手。我使用Rails4、Eclipse、Windows Xp(sp3)和mysql5.6,当我想要单击“显示”链接时,会出现上述错误 未定义的方法“post\u comments\u path”# 提取的源(第25行附近): 添加评论: …这是25号线,ruby-on-rails-4,Ruby On Rails 4,我有两个模型 post.rb 类Post


我有两个模型 post.rb 类Post标题:

文本:

评论 评论者:

评论:

添加评论:


|
我在模型中使用关联每当我遇到问题时,我都会使用关联,我对ruby和rails都是新手。我使用Rails4、Eclipse、Windows Xp(sp3)和mysql5.6,当我点击show链接时,我遇到了上述错误。错误是我在模型中使用了关联。每当我应用关联时,我遇到了问题,我是ruby和rails的新手。我使用Rails4、Eclipse、Windows Xp(sp3)和mysql5.6,当我想要单击显示链接时,我收到了上述错误。错误是

我感觉您错过了关于嵌套资源的部分。例如:

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 %>
这意味着resources:comments生成的路由嵌套在您的帖子中,这会导致URL如下所示:

resources :posts do
  resources :comments
end

我正在从guides.rubyonrails.org/getting_started.html第7.1章学习Rails4运行
rake routes
命令,在终端中,您将获得所有应用程序路径的列表。我看到了路径。但我在错误中得到错误是未定义方法“post_comments_path”我复制了guides.rubyonrails.org/getting_started.html第7.1章中的代码,请在我写博客时查看show.html.rb文件::Application.routes.draw do resources:posts do resources:comments end
/posts/1/comments