Rails Controller and namespace
namespace
Routing
config/routes.rb
namespace :admin do root 'top#index' end
controllername#actionname
We can access http://localhost:3000/admin
Controller
rails g controller admin/top
app/controllers/admin/top_controller.rb
app/views/admin/top
Action
app/controllers/admin/top_controller.rb
class Admin::TopController < ApplicationController
def index
render action: 'index' # use index template
end
end
[/ruby]
render : ERB template, app/views/admin/top/index.html.erb
<h4>Layout</h4>
<b>app/views/layouts/applicaiton.html.erb</b>
<hr>
<h3>Layout</h3>
Use <b>layout</b> in controller
app/controllers/application_controller.rb
[ruby]
layout :set_layout
private
def set_layout
if params[:controller].match(%r{\A(admin|general)/})
Regexp.last_match[1]
else
'general'
end
end
Partial Template
<div id="wrapper">
<%= render 'shared/header' %>
<div id="container">
<%= yield %>
</div>
<%= render 'shared/footer' %>
</div>
shared/header and shaerd/footer are template.
Let’s make app/views/shared, named _header.html.erb
Header, Footer
_header.html.erb
<header> </header>
_footer.html.erb
<footer> <p>Powered by Application © 2014 atmarkplant</p> </footer>
Render
<%= render 'shared/header' %> <%= render 'shaerd/footer' %>
Helper Method
render is one of Helper method
Example app/helpers/application_helper.rb
module ApplicationHelper
def document_title
if @title.present?
"#{@title} - Application"
else
'Application' end
end
end
How to call in view erb
<%= document_title %>
Asset Pipeline
Target is app/assets images, javascripts, stylesheets
Sass SCSS
Sass (Syntactically Awesome Stylesheets)
Extend style sheets .css.scss
Stylesheet setting is in app/assets/stylesheets/application.css
Example) app/assets/stylesheets/_colors.css.scss
$dark_gray: #666666; $gray: #cccccc; $light_gray: #eeeeee; $very_light_gray: #fafafa; $dark_cyan: #448888; $very_dark_cyan: darken($dark_cyan, 25%);
Use scss template
@import 'colors'; background-color: $dark_gray;
Sass
Dimension _dimentions.css.scss
narrow: 2px; $moderate: 6px; $wide: 10px; $very_wide: 20px;
How to use
@import
div {
padding: ($wide + $very_wide * 2);
}
Prepare for production
Create secret key for production
This is after Rails 4.1 API key
config/secrets.yml
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
Create security code and export SECRET_KEY_BASE
ruby -e 'require "securerandom"; print SecureRandom.hex(64)'
Export(Use output of SecureRandom.hex(64))
export SECRET_KEY_BASE=''
Database
rake db:create RAILS_ENV=production
config/environments/production.rb
config.serve_static_assets = true
Run asset precompile
rake assets:precompile
Run production mode
rails s -e production
