0

In Gem:

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

In Model:

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

In Controller:

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

In Routes:

resources :job
root 'job#index'

In View (new):

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

In Partial View (form): **** Using 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

When I run it with 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
Osp
  • 126
  • 7

1 Answers1

1

In Routes, use as:

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

—OR—

Change to singular resources => resource:

resource :job
root 'job#index'

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

—OR—

Specify the url directly:

= simple_form_for(@job, url: job_path, html: {class: 'form-horizontal'}) do |f|
SoAwesomeMan
  • 3,226
  • 1
  • 22
  • 25
  • 1
    Oh my god, you save my day with this single line of code :') I have been searched for almost a day. – Osp Jul 06 '16 at 04:48
  • @Osp You're welcome. Consider accepting this as the answer, see instructions: http://meta.stackexchange.com/a/5235/249307 – SoAwesomeMan Jul 06 '16 at 04:50