Getting started Flashcards
Explain what is the config/routes.rb file.
1. Hints routing file that does X
- The Application’s routing file which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions.
Tell me about root ‘welcome#index’
1. Tells rails to map
root ‘welcome#index’ tells Rails to map requests to the root of the application to the welcome controller’s index action and get ‘welcome/index’ tells Rails to map requests to http://localhost:3000/welcome/index to the welcome controller’s index action.
Explain what is a new resource
2.CRUD
A resource is the term used for a collection of similar objects, such as articles, people or animals. You can create, read, update and destroy items for a resource and these operations are referred to as CRUD operations.
Explain what going on in this Error message.Missing template articles/new, application/new with {locale:[:en], formats:[:html], handlers:[:erb, :builder, :coffee]}. Searched in: * “/path/to/blog/app/views”
- The first part identifies
The first part identifies what template is missing. In this case, it’s the articles/new template. Rails will first look for this template. If not found, then it will attempt to load a template called application/new. It looks for one here because the ArticlesController inherits from ApplicationController.
The next part of the message contains a hash. The :locale key in this hash simply indicates what spoken language template should be retrieved. By default, this is the English - or “en” - template. The next key, :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. The final key, :handlers, is telling us what template handlers could be used to render our template. :erb is most commonly used for HTML templates, :builder is used for XML templates, and :coffee uses CoffeeScript to build JavaScript templates.
Explain how rails searches for this template.app/views/articles/new.html.erb.
- Template called “X”, within “X” Format and Handler
The extension of this file name is key: the first extension is the format of the template, and the second extension is the handler that will be used. Rails is attempting to find a template called articles/new within app/views for the application. The format for this template can only be html and the handler must be one of erb, builder or coffee. Because you want to create a new HTML form, you will be using the ERB language. Therefore the file should be called articles/new.html.erb and needs to be located inside the app/views directory of the application.
Explain this code.
<p>
<br></br>
</p>
<p>
<br></br>
</p>
<p>
</p>
2.
Also Explain why you would make this edition
When you call form_for, you pass it an identifying object for this form. In this case, it’s the symbol :article. This tells the form_for helper what this form is for. Inside the block for this method, the FormBuilder object - represented by f - is used to build two labels and two text fields, one each for the title and text of an article. Finally, a call to submit on the f object will create a submit button for the form.
2.The form needs to use a different URL in order to go somewhere else. This can be done quite simply with the :url option of form_for. Typically in Rails, the action that is used for new form submissions like this is called “create”, and so the form should be pointed to that action.he articles_path helper tells Rails to point the form to the URI Pattern associated with the articles prefix; and the form will (by default) send a POST request to that route. This is associated with the create action of the current controller, the ArticlesController.
What happens when a form is submitted
1. The fields are
When a form is submitted, the fields of the form are sent to Rails as parameters. These parameters can then be referenced inside the controller actions, typically to perform a particular task.
Explain this.def create
render plain: params[:article].inspect
end
- The render method is
The render method here is taking a very simple hash with a key of plain and value of params[:article].inspect. The params method is the object which represents the parameters (or fields) coming in from the form. The params method returns an ActiveSupport::HashWithIndifferentAccess object, which allows you to access the keys of the hash using either strings or symbols. In this situation, the only parameters that matter are the ones from the form.