NoMethodError(未定义的方法`[]'为nil:NilClass)帖子附件问题

时间:2015-04-25 18:41:09

标签: ruby-on-rails-4

我收到此错误,我不知道为什么。我的帖子仍然保存到数据库中,即使它抛出并出错。它与post_attachments有关,但我不知道如何修复它。任何帮助表示赞赏!

这是我的PostController创建方法,错误:

def create
    @trail = Trail.find(params[:trail_id])
    @post = current_user.posts.build(params.require(:post).permit(:body, post_attachments_attributes: [:id, :post_id, :gallery]))
    @post.trail = @trail
    @new_post = Post.new
    authorize @post

if @post.save
  params[:post_attachments]['gallery'].each do |a|  
    @post_attachment = @post.post_attachments.create!(:gallery => a, :post_id => @post.id)
  end
  flash[:notice] = "Post was saved!"
  redirect_to @post.trail
else
  flash[:error] = "Error creating the post. Please try again."
  render :new
end

错误发生在params[:post_attachments]['gallery'].each do |a|

0 个答案:

没有答案