Cake views are just presentation-flavored fragments that fit inside
an application’s layout. For most applications they’re HTML mixed
with PHP, but they may end up as XML, CSV, or even binary data.
Layouts are presentation code that is wrapped around a view, and
can be defined and switched between
if the HTTP method of the
request was POST, try to save the data using the Post model.
view templates are written in a language called ERB (Embedded Ruby) which is converted by the request cycle in Rails before being sent to the user.
Each action's purpose is to collect information to provide it to a view.
A view's purpose is to display this information in a human readable format.
routing file which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions.
You can create, read, update and destroy items for a resource and these operations are referred to as CRUD operations
A controller is simply a class that is defined to inherit from ApplicationController.
If not found, then it will attempt to load a template called application/new. It looks for one here because the PostsController inherits from ApplicationController
:formats specifies the format of template to be served in response. The default format is :html, and so Rails is looking for an HTML template.
:handlers, is telling us what template handlers could be used to render our template.
When you call form_for, you pass it an identifying object for this
form. In this case, it's the symbol :post. This tells the form_for
helper what this form is for.
that the action attribute for the form is pointing at /posts/new
When a form is submitted, the fields of the form are sent to Rails as parameters.
parameters can then be referenced inside the controller actions, typically to perform a particular task
params method is the object which represents the parameters (or fields) coming in from the form.
Active Record is smart enough to automatically map column names to
model attributes,
Rails uses rake commands to run migrations,
and it's possible to undo a migration after it's been applied to your database
every Rails model can be initialized with its
respective attributes, which are automatically mapped to the respective
database columns.
migration creates a method named change which will be called when you
run this migration.
The action defined in this method is also reversible, which
means Rails knows how to reverse the change made by this migration, in case you
want to reverse it later
Migration filenames include a timestamp to ensure that they're processed in the
order that they were created.
@post.save returns a boolean indicating
whether the model was saved or not.
prevents an attacker from
setting the model's attributes by manipulating the hash passed to the model.
If you want to link to an action in the same controller, you don't
need to specify the :controller option, as Rails will use the current
controller by default.
inherits from
ActiveRecord::Base
Active Record supplies a great deal of functionality to
your Rails models for free, including basic database CRUD (Create, Read, Update,
Destroy) operations, data validation, as well as sophisticated search support
and the ability to relate multiple models to one another.
Rails includes methods to help you validate the data that you send to models
Rails can validate a variety of conditions in a model,
including the presence or uniqueness of columns, their format, and the
existence of associated objects.
redirect_to will tell the browser to issue another request.
rendering is done within the same request as the form submission
Each request for a
comment has to keep track of the post to which the comment is attached, thus the
initial call to the find method of the Post model to get the post in question.
pluralize is a rails helper that takes a number and a string as its
arguments. If the number is greater than one, the string will be automatically pluralized.
The render method is used so that the @post object is passed back to the new template when it is rendered.
The method: :patch option tells Rails that we want this form to be submitted
via the PATCH HTTP method which is the HTTP method you're expected to use to
update resources according to the REST protocol.
it accepts a hash containing the attributes
that you want to update.
field_with_errors. You can define a css rule to make them
standout
belongs_to :post, which sets up an Active Record association
creates comments as a nested resource within posts
call destroy on Active Record objects when you want to delete
them from the database.
Rails allows you to
use the dependent option of an association to achieve this.
store all external data as UTF-8
you're better off
ensuring that all external data is UTF-8
use UTF-8 as the internal storage of your database
Rails defaults to converting data from your database into UTF-8 at
the boundary.
:patch
By default forms built with the form_for helper are sent via POST
The :method and :'data-confirm'
options are used as HTML5 attributes so that when the link is clicked,
Rails will first show a confirm dialog to the user, and then submit the link with method delete.
This is done via the JavaScript file jquery_ujs which is automatically included
into your application's layout (app/views/layouts/application.html.erb) when you
generated the application.
Without this file, the confirmation dialog box wouldn't appear.
just defines the partial template we want to render
As the render
method iterates over the @post.comments collection, it assigns each
comment to
a local variable named the same as the partial
use the authentication system
require and permit
the method is often made private to make sure
it can't be called outside its intended context.
standard CRUD actions in each
controller in the following order: index, show, new, edit, create, update
and destroy.
must be placed
before any private or protected method in the controller in order to work
can overwrite the default label by passing it to the input method
configure the html
of any of them
disable labels, hints or error
add a hint,
an error, or even a placeholder
add an inline label
pass any html attribute straight to the input, by using the :input_html
option
use the :defaults option in simple_form_fo
Simple Form generates a wrapper div around your label and input by default, you can pass
any html attribute to that wrapper as well using the :wrapper_html option,
By default all inputs are required
the required property of any input can be overwritten
Simple Form will look at the column type in the database and use an
appropriate input for the column
lets you overwrite the default input type it creates
can also render boolean
attributes using as: :select to show a dropdown.
give the :disabled option to Simple Form, and it'll automatically mark
the wrapper as disabled with a CSS class
Simple Form accepts same options as their corresponding input type helper in Rails
Any extra option passed to these methods will be rendered as html option.
use label, hint, input_field, error and full_error helpers
to strip away all the div wrappers around the <input> field
is to use f.input_field
changing boolean_style from default value :nested to :inline
overriding the :collection option
Collections can be arrays or ranges, and when a :collection is given the :select input will be
rendered by default
Other types of collection
are :radio_buttons and :check_boxes
label_method
value_method
Both of these options also accept
lambda/procs
define a to_label method on your model as Simple Form will search for
and use :to_label as a :label_method first if it is found
create grouped collection selects, that will use the html optgroup tags
Grouped collection inputs accept the same :label_method and :value_method options
group_method
group_label_method
configured with a default value to be used on the site through the
SimpleForm.country_priority and SimpleForm.time_zone_priority helpers.
association
association
render a :select input for choosing the :company, and another
:select input with :multiple option for the :roles
all options available to :select,
:radio_buttons and :check_boxes are also available to association
declare different labels and values
the association helper is currently only tested with Active Record
f.input
f.select
create a <button> element
simple_fields_for
Creates a collection of radio inputs with labels associated
Creates a collection of checkboxes with labels associated
collection_radio_buttons
collection_check_boxes
associations in your model
Role.all
the html element you will get for each attribute
according to its database definition
redefine existing Simple Form inputs by creating a new class with the same name
Simple Form uses all power of I18n API to lookup labels, hints, prompts and placeholders
specify defaults for all
models under the 'defaults' key
Simple Form will always look for a default attribute translation under the "defaults" key if no
specific is found inside the model key
Simple Form will fallback to default human_attribute_name from Rails when no other
translation is found for labels.
Simple Form will only do the lookup for options if you give a collection
composed of symbols only.
"Add %{model}"
translations for labels, hints and placeholders for a namespaced model, e.g.
Admin::User, should be placed under admin_user, not under admin/user
This difference exists because Simple Form relies on object_name provided by Rails'
FormBuilder to determine the translation path for a given object instead of i18n_key from the
object itself.
configure how your components will be rendered using the wrappers API
optional
unless_blank
By default, Simple Form will generate input field types and attributes that are supported in HTML5
The HTML5
extensions include the new field types such as email, number, search, url, tel, and the new
attributes such as required, autofocus, maxlength, min, max, step.
If you want to have all other HTML 5 features, such as the new field types, you can disable only
the browser validation
add novalidate to a specific form by setting the option on the form itself