Skip to content

Instantly share code, notes, and snippets.

@amyroi
Last active September 8, 2015 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amyroi/7ee587f4394cceaed1cd to your computer and use it in GitHub Desktop.
Save amyroi/7ee587f4394cceaed1cd to your computer and use it in GitHub Desktop.
SimpleForm + ActiveModel:validation
# app/models/account.rb
class Account
include ActiveModel::Model
validates :name, presence: true
def initialize
# ~
end
end
# app/controllers/accounts_controller.rb
class AccountsController < ApplicationController
def new
@account = Account.new
end
def create
@account = Account.new(params[:account])
if @account.valid?
@account.save
else
render :new
end
end
end
# app/views/account/_form.html.haml
## :accountではなく@accountにする
= simple_form_for(@account, url: '/accounts') do |f|
= f.error_notification
.form-inputs
= f.input :name
.form-actions
= f.button :submit, 'create', class: 'btn-raised'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment