NoMethodError位于/ authors / posts。未定义的方法[[] ='for nil:NilClass

时间:2019-09-28 10:13:15

标签: ruby-on-rails-5

我目前正在从事StephenFiser(https://github.com/StephenFiser/FrogBlog)进行的较旧的项目,用于学习目的,并了解如何提高自己的技能(解决问题和调试问题)。我尝试将Active Storage For Image Uploads添加到项目中,除了gravatar_image_url之外,没有其他图像文件上传。因此,我尝试使用主动存储。在尝试添加Active Storage之前,该项目运行良好。现在,我添加了活动存储并运行了迁移。拜托,我在这里想念什么?我已经尝试了所有可能的解决方案和可以在网上找到的问题。

但是,当我尝试从作者的仪表板添加新帖子时,我得到了:

创建

app / controllers / authors / posts_controller.rb,第39行

34结尾

35#POST /帖子

36#POST /posts.json

37 38 def创建

39 @post = current_author.posts.new(post_params)

40
41 response_to |格式|

42 if @ post.save

43 format.html {redirect_to authors_post_path(@post),注意:“帖子已成功创建。” }

44 format.json {渲染:显示,状态::创建,位置:@post}

发布模型

class Post

扩展FriendlyId   friendly_id:title,使用::slugged

属于:作者   has_one_attached:image

PER_PAGE = 3

scope:most_recent,-> {order(published_at::desc)}   范围:已发布,-> {其中(已发布:true)}   范围:recent_paginated,->(页面){most_recent.paginate(页面:页面,每页:PER_PAGE)}   作用域:with_tag,->(tag){如果tag.present存在tag_with(tag)? }

scope:list_for,->(页面,标签)     last_paginated(页面).with_tag(标签)   结束

def should_generate_new_friendly_id?     title_changed?   结束

def display_day_published     如果published_at.present?       “已发布#{published_at.strftime('%-b%-d,%Y')}”     其他       “尚未发布。”     结束   结束

def发布     更新(已发布:true,已发布时间:Time.now)   结束

def未发布     更新(已发布:false,已发布_at:无)   结束

结束

后控制器

模块作者   类PostsController

# GET /posts
# GET /posts.json
def index
  @posts = current_author.posts.most_recent
end

# GET /posts/1
# GET /posts/1.json
def show
end

# GET /posts/new
def new
  @post = current_author.posts.new
end

# GET /posts/1/edit
def edit
end

def publish
  @post.publish
  redirect_to authors_posts_url
end

def unpublish
  @post.unpublish
  redirect_to authors_posts_url
end

# POST /posts
# POST /posts.json
def create
  @post = current_author.posts.new(post_params)      

  respond_to do |format|
    if @post.save
      format.html { redirect_to authors_post_path(@post), notice: 'Post was successfully created.' }
      format.json { render :show, status: :created, location: @post }
    else
      format.html { render :new }
      format.json { render json: @post.errors, status: :unprocessable_entity }
    end
  end
end

# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
  respond_to do |format|
    if @post.update(post_params)
      format.html { redirect_to authors_post_path(@post), notice: 'Post was successfully updated.' }
      format.json { render :show, status: :ok, location: @post }
    else
      format.html { render :edit }
      format.json { render json: @post.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /posts/1
# DELETE /posts/1.json
def destroy
  @post.destroy
  respond_to do |format|
    format.html { redirect_to authors_posts_url, notice: 'Post was successfully destroyed.' }
    format.json { head :no_content }
  end
end

private
  # Use callbacks to share common setup or constraints between actions.
  def set_post
    @post = current_author.posts.friendly.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def post_params
    params.require(:post).permit(:title, :body, :description, :tag_list, :image)
  end

结束

结束

0 个答案:

没有答案