Rails上的表单错误(没有脚手架)

时间:2016-08-04 15:40:34

标签: ruby-on-rails forms rest routes namespaces

宝石文件:

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'

控制器:( rails g控制器学院/学生)

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

路线:

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

错误:

Academy :: Student#new中的NoMethodError(当我尝试运行时:http://localhost:3000/academy/student/new

未定义的方法`academy_students_path&#39;对于#&lt;#:0x007fd586ab8278&gt;你的意思是? academy_student_path            academy_student_index_path            academy_student_url            academy_courses_path

注意:

此控制器&amp;模型生成没有脚手架(当我使用脚手架,其工作)

正在使用更新。

2 个答案:

答案 0 :(得分:0)

根据命名约定,控制器名称应为复数。 在您的情况下,students_controller

student_controller.rb重命名为students_controller.rb,将您的控制器类名称更改为

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

另外,在路线中进行更改

<强>的routes.rb

resources :students

会给你

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

答案 1 :(得分:0)

您正在尝试获取academy/student#index 在您给定的路线详细信息中,您应该看到没有academy_students_path的名称,而academy/student#index则有academy_student_index_path

使用academy_student_index_path代替academy_students_path

相关问题