Last active
September 8, 2015 03:10
-
-
Save amyroi/7ee587f4394cceaed1cd to your computer and use it in GitHub Desktop.
SimpleForm + ActiveModel:validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/account.rb | |
class Account | |
include ActiveModel::Model | |
validates :name, presence: true | |
def initialize | |
# ~ | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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