错误消息“ Posts#new中的NoMethodError”

时间:2019-02-06 02:53:48

标签: ruby-on-rails ruby

我正在用Ruby on Rails制作一个简单的bbs,用日语对不起。 我刚收到此错误消息

  

“帖子中的NoMethodError #new”

     

在第7行出现的位置显示/Users/igarashinobuo/portfolio/cpmybbs/app/views/posts/new.html.erb:

     

nil:NilClass的未定义方法“ posts”

  </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 %

您能帮我调试吗?

1 个答案:

答案 0 :(得分: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