如何在没有脚手架命令的情况下进行Rails CRUD

How to do Rails CRUD without scaffolding command

在Gem中:

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

模型中:

ActiveRecord::Schema.define(version: 20160706040748) do
  create_table "jobs", force: :cascade do |t|
    t.string   "title"
    t.text     "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end
end

在控制器中:

class JobController < ApplicationController

before_action :find_job, only: [:show, :edit, :update, :destroy]

  def index
  end

  def show
  end

  def new
    @job = Job.new
  end

  def create
    @job = Job.new(jobs_params)
    if @job.save
        redirect_to @job
    else
        render "New"
    end
  end

  def edit
  end

  def update
  end

  def destroy
  end

  private

  def jobs_params
    params.require(:job).permit(:title, :description)
  end

  def find_job
    @job = Job.find(params[:id])
  end

end

在路线中:

resources :job
root 'job#index'

在视图中(新):

%h1 New Job
= render 'form'
= link_to "Back", root_path

在部分视图中(表单):**** 使用 simple_form gem ***

= simple_form_for(@job, html: { class: 'form-horizontal'}) do |f|
= f.input :title, label: "Course Title"
= f.input :description, label: "Course description"
%br/
= f.button :submit

当我运行它与http://localhost:3000/job/new

NoMethodError in Job#new

undefined method `jobs_path' for #<#<Class:0x007ffcf6241328>:0x007ffcfe176bc0>

= simple_form_for(@job, html: { class: 'form-horizontal'}) do |f|
= f.input :title, label: "Course Title"
= f.input :description, label: "Course description"
%br/
= f.button :submit

在路线中,使用as:

resources :job, as: :jobs
root 'job#index'

—或—

改为单数 resources => resource:

resource :job
root 'job#index'

参见:http://guides.rubyonrails.org/routing.html#singular-resources

—或—

直接指定url:

= simple_form_for(@job, url: job_path, html: {class: 'form-horizontal'}) do |f|