Osp
Osp

Reputation: 126

Form error on Rails (Without Scaffold)

Gem file :

source 'https://rubygems.org'
ruby "2.3.1"
gem 'rails', '4.2.3'
gem 'bootstrap-sass', '~> 3.3.6'
gem 'font-awesome-rails', '4.3.0.0'
gem 'sass-rails', '~> 5.0.4'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'

group :doc do
  gem 'sdoc', require: false
end

group :development, :test do
  gem 'byebug', platform: :mri
  gem 'sqlite3'
end

group :development do
  gem 'web-console'
  gem 'listen', '~> 3.0.5'
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :production do
  gem 'unicorn'
  gem 'pg'
  gem 'rails_12factor'
end

gem 'simple_form', '~> 3.2', '>= 3.2.1'
gem 'haml', '~> 4.0', '>= 4.0.7'

Controller: (rails g controller Academy/Student)

class Academy::StudentController < ApplicationController
  before_action :find_course, only: [:show, :edit, :update, :destroy]

  def index
    @academy_students = Academy::Student.all.order("created_at DESC")
    # render json: Academy::Student.all
  end

  def new
    @academy_student = Academy::Student.new
  end

  def show
    # list = Academy::Student.find(params[:id])
    # render json: list
  end

  def create
    if @academy_student = Academy::Student.create(academy_student_params)
      redirect_to @academy_student
    else
      render :new
    end
  end

  def edit
  end

  def update
    if @academy_student.update(academy_student_params)
      redirect_to @academy_student
    else
      render :edit
    end
  end

  def destroy
    @academy_student.destroy
    redirect_to @academy_student
  end

  private

  def find_course
    @academy_student = Academy::Student.find(params[:id])
  end

  def academy_student_params
    params.require(:academy_student).permit(:student_code, :student_name, :student_gender, :student_email, :student_phone, :student_address, :student_degree, :student_grade)
  end
end

routes :

namespace :academy do
    resources :student
end

academy_student_index   GET    /academy/student(.:format)                       academy/student#index
                        POST   /academy/student(.:format)                       academy/student#create
new_academy_student     GET    /academy/student/new(.:format)                   academy/student#new
edit_academy_student    GET    /academy/student/:id/edit(.:format)              academy/student#edit
academy_student         GET    /academy/student/:id(.:format)                   academy/student#show
                        PATCH  /academy/student/:id(.:format)                   academy/student#update
                        PUT    /academy/student/:id(.:format)                   academy/student#update
                        DELETE /academy/student/:id(.:format)                   academy/student#destroy

_form.html.haml

= simple_form_for(@academy_student, html: { class: 'form-horizontal'}) do |f|
  = f.error_notification
  .form-inputs
    = f.input :student_code, label: 'Student Code:'
    = f.input :student_name, label: 'Student Name:'
    = f.input :student_gender, label: 'Student Gender:'
    = f.input :student_email, label: 'Student Email:'
    = f.input :student_phone, label: 'Student Phone:'
    = f.input :student_address, label: 'Student Address:'
    = f.input :student_degree, label: 'Student Degree:'
    = f.input :student_grade, label: 'Student Grade:'
    = f.button :submit

Error:

NoMethodError in Academy::Student#new (when i try to run : http://localhost:3000/academy/student/new)

undefined method `academy_students_path' for #<#:0x007fd586ab8278> Did you mean? academy_student_path academy_student_index_path academy_student_url academy_courses_path

Note:

This controller & model are generate without scaffold (when i use scaffold, its work)

Its working with update.

Upvotes: 0

Views: 73

Answers (2)

Deepak Mahakale
Deepak Mahakale

Reputation: 23661

According to the naming conventions, controller name should be plural. Which in your case is students_controller

rename student_controller.rb to students_controller.rb change your controller class name to

class Academy::StudentsController < ApplicationController
  # ...
end

also, make changes in routes

routes.rb

resources :students

which will give you

academy_students        GET    /academy/students(.:format)                       academy/students#index
                        POST   /academy/students(.:format)                       academy/students#create
new_academy_students    GET    /academy/students/new(.:format)                   academy/students#new
edit_academy_students   GET    /academy/students/:id/edit(.:format)              academy/students#edit
academy_students        GET    /academy/students/:id(.:format)                   academy/students#show
                        PATCH  /academy/students/:id(.:format)                   academy/students#update
                        PUT    /academy/students/:id(.:format)                   academy/students#update
                        DELETE /academy/students/:id(.:format)                   academy/students#destroy

Upvotes: 0

Awlad Liton
Awlad Liton

Reputation: 9351

You are trying get academy/student#index In your given routes details you should see there is no name of academy_students_path And for the academy/student#index you have academy_student_index_path.

Use academy_student_index_path instead academy_students_path

Upvotes: 0

Related Questions