错误消息“NoMethodError在帖子#新”

问题描述 投票:0回答:1

我对Ruby on Rails的,在日本对不起制作一个简单的BBS。我刚刚得到这个错误信息

“NoMethodError在帖子#新”

显示/Users/igarashinobuo/portfolio/cpmybbs/app/views/posts/new.html.erb在行#7提出:

未定义的方法`岗位为无:NilClass

  </div>
  <div class="mt-10 pd-20">
    <% @topic.posts.order(:id).each.with_index(1) do |post , idx| %>
      <p>
        <%= idx %>
        <%= post.name %>

发生此错误,当我想访问“http://localhost:3002/topics/22/posts/new”。图22是柱的数目。新页面是微柱的页面。

# app/views/posts/new.html.erb
<div class="text-center bg-light font-weight-normal pt-3">
  <a href=/topics>Chatty</a>
</div>
  <div class="text-center pb-20 mt-10">
  </div>
  <div class="mt-10 pd-20">
    <% @topic.posts.order(:id).each.with_index(1) do |post , idx| %>
      <p>
        <%= idx %>
        <%= post.name %>
        <%= post.body %>
      </p>
    <% end %>
  </div>
  <div class="newpost my-auto mb-60">
    <h4>新規書き込み</h4>
    <%= form_for @post, url: topic_posts_path(@topic) do |f| %>
      <div class="post_area">
        <p><%= f.text_field :name %></p>
        <p><%= f.label :name %></p>
        <p><%= f.text_field :name %></p>
        <p><%= f.label :body %></p>
        <p><%= f.text_area :body %></p>
        <%= f.submit %>
      </div>
    <% end %>

# posts_controller.rb
class PostsController < ApplicationController
  def new
    @post = Post.new
  end

  def create
    @post = @topic.posts.build(set_topic)
    if @post.save
      redirect_to new_topic_post_path(@topic)
    else
      render :new
    end
  end

  private

  def set_topic
    @topic = Topic.find(params[:topic_id, :title])
  end

  def post_params
    params.require(:post).permit(:name, :body)
  end
end

# error log
app/views/posts/new.html.erb:7:in `_app_views_posts_new_html_erb__4195515470978617912_70184702044840'
Started GET "/topics/22/posts/new" for ::1 at 2019-02-06 11:52:00 +0900
Processing by PostsController#new as HTML
  Parameters: {"topic_id"=>"22"}
  Rendering posts/new.html.erb within layouts/application
  Rendered posts/new.html.erb within layouts/application (3.3ms)
Completed 500 Internal Server Error in 11ms (ActiveRecord: 0.0ms)

ActionView::Template::Error (undefined method `posts' for nil:NilClass):
    4:   <div class="text-center pb-20 mt-10">
    5:   </div>
    6:   <div class="mt-10 pd-20">
    7:     <% @topic.posts.order(:id).each.with_index(1) do |post , idx| %>
    8:       <p>
    9:         <%= idx %>
   10:         <%= post.name %

你能帮我好心调试?

ruby-on-rails ruby
1个回答
1
投票

您需要添加一些对控制器的更改:

  • 过滤器之前添加set_topic,找到@topic的每一个动作
  • 改变set_topic了一下,由于某些原因,你试图通过寻找话题 params[:topic_id, :title],这是访问的哈希值无效路
  • 使用post_params建立一个新的职位

结果:

class PostsController < ApplicationController
  before_action :set_topic

  def new
    @post = Post.new
  end

  def create
    @post = @topic.posts.build(post_params)
    if @post.save
      redirect_to new_topic_post_path(@topic)
    else
      render :new
    end
  end

  private

  def set_topic
    @topic = Topic.find(params[:topic_id])
  end

  def post_params
    params.require(:post).permit(:name, :body)
  end
end
© www.soinside.com 2019 - 2024. All rights reserved.