ActiveRecord::RecordNotFound in ArticlesController#show Couldn't find Article without an ID

前端 未结 1 709
遇见更好的自我
遇见更好的自我 2021-01-28 11:26

I am trying to submit some data to db and its ok but when i am trying to retrieve those data show that Couldn\'t find Article without an ID.ils 4.0.1. I am using ruby 2.0.0 and

1条回答
  •  忘了有多久
    2021-01-28 11:42

    You are redirecting before actually saving your article.

    This:

    def create
      @article = Article.new(article_params)
      redirect_to @article
      @article.save
    end
    

    should be:

    def create
      @article = Article.new(article_params)
      @article.save
      redirect_to @article      
    end
    

    And if you want to add some error handling as well:

    def create
      @article = Article.new(article_params)
      if @article.save
        redirect_to @article
      else
        render :new
    end
    

    0 讨论(0)
提交回复
热议问题