路由错误未初始化常量 SubmitsController(尝试使用 Carrierwave 上传文档)

Routing Error uninitialized constant SubmitsController (Trying to upload document using Carrierwave)

我正在构建我的第一个有用的 Rails 应用程序,供教师 post 作业和学生使用 Carrierwave 在网站上上传他们的作品 (jpeg|pdf)。

我要复制的教程:http://www.tutorialspoint.com/ruby-on-rails/rails-file-uploading.htm

错误:“#<#:0x007f7260dbc7a0> 的“未定义方法 submits_path”是您的意思吗?submit_path”

错误消息:提取的源代码(大约第 14 行):

13    <div class = "well">
14       <%= form_for @submits, html: { multipart: true } do |f| %>
15          <%= f.label :name %>
16          <%= f.text_field :name %>
17          <%= f.label :attachment %>

型号:submit.rb

class Submit < ActiveRecord::Base
    mount_uploader :attachment, AttachmentUploader # Tells rails to use this uploader for this model.
   validates :name, presence: true # Make sure the owner's name is present.
end

控制器:submit_controller.rb

class SubmitController < ApplicationController
 def index
      @submits = Submit.all
 end

   def new
      @submit = Submit.new
   end

   def create
      @submit = Submit.new(submit_params)

      if @submit.save
         redirect_to submits_path, notice: "The assignment #{@submit.name} has been uploaded."
      else
         render "new"
      end

   end

   def destroy
      @submit = Submit.find(params[:id])
      @submit.destroy
      redirect_to submits_path, notice:  "The assignment #{@submit.name} has been deleted."
   end

   private
      def submit_params
      params.require(:submit).permit(:name, :attachment)
      end

end

路线:

Rails.application.routes.draw do

resources :submit, only: [:index, :new, :create, :destroy]

  get 'welcome/index'

  get 'welcome/about'

  root 'welcome#index'
end

架构:

ActiveRecord::Schema.define(version: 20160903040246) do

  create_table "submits", force: :cascade do |t|
    t.string   "name"
    t.string   "attachment"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

rake routes :

       Prefix Verb   URI Pattern              Controller#Action
 submit_index GET    /submit(.:format)        submit#index
              POST   /submit(.:format)        submit#create
   new_submit GET    /submit/new(.:format)    submit#new
       submit DELETE /submit/:id(.:format)    submit#destroy
welcome_index GET    /welcome/index(.:format) welcome#index
welcome_about GET    /welcome/about(.:format) welcome#about
         root GET    /                        welcome#index

提前感谢这里所有愿意伸出援助之手的好人

您正在使用 @submits 而不是 @submit,此处:

<%= form_for @submits, html: { multipart: true } do |f| %>

应该是:

<%= form_for @submit, html: { multipart: true } do |f| %>

哪个是你在controller中创建的变量,这里:

def new
  @submit = Submit.new
end

已编辑答案

我将退后一步,指出您因 Rails 公约而面临的一些问题。

Rails 理念是约定优于配置。这意味着如果您遵循一些约定,Rails 将能够在不配置任何(或几乎任何)的情况下执行您期望的操作。

除了我上面指出的错误之外,您在答案中列出的所有其他错误都是因为您以单数 SubmitController 而不是 SubmitsController 创建了控制器。这改变了 Rails 预期的约定,因此您必须手动进行一些配置。

由于您正在学习教程或其他内容,我的建议是退后一步并使用正确的约定重新创建您的控制器。