重复出现 RecordNotFound 错误

Recurring RecordNotFound error

我的索引页没有显示我的任何 post(此处命名合适)

然而,在进一步的故障排除之后,我的 post 似乎根本没有被创建!

我尝试添加一个全新的 post(fit),它给了我一条成功消息,但是我的索引没有显示说 post 并且在尝试导航到 /fits/X 之后它给了我一个 ActiveRecord::RecordNotFound(下面的错误)

Extracted source (around line #56):            
def set_fit
    @fit = Fit.find(params[:id])
end

我的控制器看起来像这样

def new
    @fit = current_user.fits.build
end

def create
    if @fit = current_user.fits.build(fit_params)
        flash[:success] = "Your fit has been created!"
        redirect_to fits_path
    else
        flash.now[:alert] = "Error adding fit! Please check fields."
        render :new
    end
end

这是我的模型

class Fit < ActiveRecord::Base
 validates :user_id, presence: true
 validates :image, presence: true

 belongs_to :user

 has_many :comments, dependent: :destroy

 has_attached_file :image, styles: { :medium => "600x" }
 validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end

我也已将此添加到我的 routes.rb。这可能是问题所在吗? (因为还没有评论)

Rails.application.routes.draw do
 devise_for :users, :controllers => { registrations: 'registrations' }  
 resources :fits do
   resources :comments
 end

 root 'fits#index'
end

在过去的一个小时里,我一直在为此摸不着头脑,并且正在努力解决这个问题!尽管我确信这是小事,但我完全忽略了它。

如果您需要访问任何其他内容,我已将其推送到 github 此处为您服务。 https://github.com/thgilartlU/MFID

提前致谢!

我认为问题出在您的创建方法中。您没有保存记录。创建变量,然后确保保存它。

def create
  @fit = current_user.fits.build(fit_params)
  if @fit.save
      flash[:success] = "Your fit has been created!"
      redirect_to fits_path
  else
      flash.now[:alert] = "Error adding fit! Please check fields."
      render :new
  end
end