PostsController中的NoMethodError #create 未定义的方法`帖子'为零:NilClass
帖子#控制器
class PostsController < ApplicationController
# before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
before_action :find_postable, except: [:index]
def index
@posts = Post.all.order('created_at DESC')
end
def new
@post = Post.new
end
def create
@post = @postable.posts.new post_params
if @post.save
redirect_to :back, notice: 'Your comment was successfully posted!'
else
redirect_to :back, notice: "Your comment wasn't posted!"
end
end
def destroy
Post.find(params[:id]).destroy
redirect_to posts_path#, notice: "Post successfully deleted"
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update_attributes(student_params)
redirect_to post_path
else
redirect_to edit_post_path
end
end
def show
@post = Post.find (params[:id])
end
private
def post_params
params.require(:post).permit(:content, :image, :document, :audio, :video, :classroom_id)
end
def correct_user
@post = current_user.posts.find_by(id: params[:id])
redirect_to root_url if @post.nil?
end
def find_postable
@postable = Post.find_by_id(params[:post_id]) if params[:post_id]
@postable = Classroom.find_by_id(params[:classroom_id]) if params[:classroom_id]
end
这是帖子模型
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :classroom
belongs_to :postable, polymorphic: true
has_many :posts, as: :postable
end
这是教室模型(它属于班级,这是在新的帖子形式中选择的#34;
class Classroom < ActiveRecord::Base
has_many :posts, as: :postable
belongs_to :teachers
# belongs_to :students
Schema.rb中的帖子
create_table "posts", force: :cascade do |t|
t.text "content"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "classroom_id"
t.integer "postable_id"
t.string "postable_type"
end
我尝试了许多没有成功的事情,我不太清楚为什么我会收到这个错误。我正在关注视频并试图将其应用到我现有的帖子模型中,但仍然遇到问题。感谢
新职表:
<%= form_for :post, html: { multipart: true, class: "post-form" }, url: posts_path do |f| %>
<h4>Choose a Classroom</h4>
<div class="form-group">
<%= f.collection_select :classroom_id, current_user.classrooms, :id, :name %>
</div>
<h4>Write a post</h4>
<div class="form-group">
<%= f.text_area :content, class: "form-control" %>
</div>
<h4>Attach Files</h4>
<div class="form-group">
<%= f.file_field :image %>
</div>
<%= f.submit "Add Post", class: "btn btn-primary" %>
<% end %>
回复帖子
<li>
<%= post.content %> -
<%= form_for [post, Post.new] do |f| %>
<%= f.text_area :content, placeholder: "Add a Reply" %><br/>
<%= f.submit "Reply" %>
<% end %>
</li>
答案 0 :(得分:1)
错误在find_postable
方法中,特别是在此行中:
@postable = Classroom.find_by_id(params[:classroom_id]) if params[:classroom_id]
您正尝试从classroom_id
(即params
)获取params[:classroom_id]
,但该密钥(classroom_id
)存在于您的参数哈希中的post
项中:
Parameters: {
"utf7"=>"✓",
"authenticity_token"=>"bunchofletters==",
"post"=>{
"classroom_id"=>"1",
"content"=>"hello"
},
"commit"=>"Add Post"
}
要解决此问题,请在params[:classroom_id]
方法中将params[:post][:classroom_id]
更改为find_postable
:
def find_postable
@postable = Post.find_by_id(params[:post_id]) if params[:post_id]
@postable = Classroom.find_by_id(params[:post][:classroom_id]) if params[:classroom_id]
end