Skip to content

Instantly share code, notes, and snippets.

@kayline
Last active December 18, 2015 00:59
Show Gist options
  • Save kayline/5700416 to your computer and use it in GitHub Desktop.
Save kayline/5700416 to your computer and use it in GitHub Desktop.

Views: Sinatra vs Rails

###Auto-Matching### Inside a given controller, each action will by default render a template with the same name as the action

class BooksController < ApplicationController
  def index
    #will automatically render app/views/books/index.html.erb
  end
end
Task Sinatra Rails Notes
return a page to the browser erb :index render :index OR render "index" many other variations including render :action => :index
create an html page with embedded ruby .erb file we call a "page" or "view" .html.erb file called a "template" note that a template is a full-blown view, NOT a layout
to return JSON to an AJAX call @product_hash.to_json render :json => @product automatically includes all readable attributes of @product
return a specific status code (e.g. 404 Not Found) route must return the integer value (404, 500, etc) render :status => 500 OR render :status => :forbidden, both integers and symbols work you can return a template AND a status, e.g. render :error_page, :status => 500
return html with no layout erb :index, :layout => false render :index, :layout => false can also specify a particular layout file e.g. render :index, layout => '/my_layout'

Layouts: Sinatra vs Rails

###Auto-Matching###

  • Each controller will look for a matching layout file and apply it to all pages rendered by actions in that controller
class BooksController < ApplicationController
    #will apply app/views/layouts/books.html.erb for all actions in BooksController
end
Task Sinatra Rails Notes
find the layout file same folder as the view .erb files in sub-folder app/views/layouts/my_layout.html.erb can be .html.erb or .builder file
use a non-default layout I'm not sure... add layout "other_layout" to your controller looks in app/views/layouts for a file named other_layout.html.erb OR other_layout.builder
create default layout for entire app create a layout.erb file in the views folder save the default layout in app/views/layout/application.html.erb if a controller doesn't find a layout matching its name, it will look for application.html.erb
force all controller to use a single layout, even if they find a matching layout file NA declare layout "main" in ApplicationController and save the layout as app/views/layouts/main.html.erb this will override the default layout matching of each controller. A specific controller can still use a different layout, but you have to declare it specifically in the controller

##Partials

Type Sinatra Rails Notes
Partials <%= erb :_menu %> <%= render "menu" %> the underscore is implied, such that this will render the file named _menu.html.erb
Partial from another directory <%= erb :_menu %> <%= render "shared/menu" %> ---
Partial layouts --- <%= render :partial => "link_area", :layout => "graybar" %> Note that explicitly specifying :partial is required when passing additional options such as :layout. Layouts also follow the leading underscore convention, e.g., _graybar.html.erb
Pass local variables via partials, part 1 --- <%= render :partial => "form", :locals => { :zone => @zone } %>
Pass local variables via partials, part 2 --- <%= render :partial => "customer", :object => @new_customer %> Every partial has a local variable with the same name as the partial (minus the underscore). You can pass an object in to this local variable via the :object option.
Shorthand --- <%= render @customer %> If you have an instance of a model to render into a partial, you can use a shorthand syntax.

###Awesome Helpers###

For more take a look here.

Truncate:
<%= truncate("I love cheese!", :length => 10) %> 
#=> I love che…

Truncate with separator in option hash:
<%= truncate("I love cheese!", :length => 10, :separator => ' ') %> 
#=> I love…

Pluralize:
I see <%= pluralize(User.count, "people") %>  
#=> I see 2 people / I see 1 person

Titleize
Her name is <%= @user.name.titleize %> 
#=> Her name is Amy Lukima

To sentence: 
Amy’s dogs names are <%= @dog_names.to_sentence %>  
#=> Amy’s dogs names are Molly, Schala and Chancellor T. Abernathy.

Time Ago:
Posted <%= time_ago_in_words @tweet.created_at %> ago  
#=> Posted 2 days ago

Number to Currency:
Price is <%= number_to_currency 13.5 %> 
#=> Price is $13.50

Highlight:
highlight('You searched for: rails', 'rails')
#=> You searched for: <strong class="highlight">rails</strong>

###Forms!###

Check this link out!

```erb When you generate a user model, this form is created for you:

<%= form_for(@user) do |f| %> #@user is a user instance. <% if @user.errors.any? %> #checking for errors

#next few lines are acting on the errors if they exist.

<%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:

  <ul>
  <% @user.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>

<% end %>

<%= f.label :name %>
#Label for input area <%= f.text_field :name %> #Input is created based on the model
<%= f.label :email %>
<%= f.text_field :email %> #Input is created based on the model, email is a smart field, client side validation!
<%= f.submit %> #Submit button
<% end %> ```

####Form Fields Quick Referance####

<%= f.select :color, ['blue', 'yellow', 'red'] %> #drop down select
<%= f.radio_button :choice, 'yes', checked: true %> #radio button prechecked.
<%= f.radio_button :choice, 'no' %> #radio button
<%= f.password_field :password %> #doesn't show password
<%= f.number_field :price %> #displays as price
<%= f.range_field :quantity %> #displays as slider on some browsers/mobile
<%= f.email_field :email %> client side validation, changes mobile browser keyboard
<%= f.url_field :website %> changes mobile browser keyboard
<%= f.telephone_field :mobile %> changes mobile browser keyboard
<%= text_area_tag(:message, "Hi, nice site", size: "24x6") %> 
<%= hidden_field_tag(:parent_id, "5") %> #hidden from user
<%= search_field(:user, :name) %> 
<%= telephone_field(:user, :phone) %>
<%= date_field(:user, :born_on) %>
<%= datetime_field(:user, :meeting_time) %>
<%= datetime_local_field(:user, :graduation_day) %>
<%= month_field(:user, :birthday_month) %>
<%= week_field(:user, :birthday_week) %>
<%= color_field(:user, :favorite_color) %>
<%= time_field(:task, :started_at) %>

#####Baby Steps#####

The basic form tag creates a form with a method of post and action to the current page.

```erb <%= form_tag do %> Form contents <% end %> ```

Model Object helper- pass in the model and the model attribute. Mainly used for editing an object.

```erb <%= text_field(:person, :name) %> #=>
<p>form tag with custom controller, action, method and class</p>
```erb
form_tag(controller: "people", action: "search", method: "get", class: "nifty_form")
 
@allenwlee
Copy link

test edit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment