Mike Stamb Mike Stamb - 1 year ago 74
Ruby Question

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

I am building my first useful Rails application for a teacher to post assignments and students to upload their work (jpeg|pdf) on the website using Carrierwave.

Tutorial I want to replicate: http://www.tutorialspoint.com/ruby-on-rails/rails-file-uploading.htm

Error: "`undefined method submits_path' for #<#:0x007f7260dbc7a0> Did you mean? submit_path"

Error message: Extracted source (around line #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 %>

model: 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.

controller: submit_controller.rb

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

def new
@submit = Submit.new

def create
@submit = Submit.new(submit_params)

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


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

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



Rails.application.routes.draw do

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

get 'welcome/index'

get 'welcome/about'

root 'welcome#index'


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


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

Thanks in advance to all the wonderful people here who are willing to lend a helping hand

Answer Source

You're using @submits insted of @submit, here:

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

Shoul be:

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

Which is the variable you have created in the controller, here:

def new
  @submit = Submit.new


I'm gonna take a step back here and point some problems you're facing due to Rails Convention.

The Rails philosophy is Convention Over Configuration. That means that if you follow some conventions, Rails will be able to do what you expect it to do it without configuring anything (or almost anything).

Besides the error I pointed above, all the other errors you have listed in the answers are because you created your controller in the singular SubmitController instead of SubmitsController. This changes the convention expected by Rails, so you would have to some configurations by hand.

Since you're following a tutorial or something, my advice is to take a step back and re-create your controller with the right conventions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download