bin/rails generate controller welcome index
Routers are defined in config/routes.rb
Rails.application.routes.draw do
resources :articles
root 'welcome#index'
end
Running bin/rake routes
will define all the standard RESTful routes
Generate the articles
controller:
bin/rails g controller articles
Defining a method in ruby:
def new
end
The first part identifies what template is missing. In this case, it is 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.
Form builder: In views/articles/new.html.erb
, use the form builder to generate the form:
<%= form_for :article, url: articles_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
Just display the params passed in, in the create controller
def create
render plain: params[:article].inspect
end
Models in Rails use a singular name, and their corresponding db tables use a plural name.
bin/rails generate model Article title:string text:text
bin/rake db:migrate
Because you are working in the development environment by default, this command will apply to the database defined in the development section of your config/database.yml file. If you would like to execute migrations in another environment, for instance in production, you must explicitly pass it when invoking the command: rake db:migrate RAILS_ENV=production.
Now, in article controller, do:
def create
@article = Article.new(params[:article])
@article.save
redirect_to @article
end
Whitelist the params. Mass assginment is a security risk:
def create
@article = Article.new(article_params)
@article.save
redirect_to @article
end
private
def article_params
params.require(:article).permit(:title, :text)
end
A frequent practice is to place the standard CRUD actions in each controller in the following order: index, show, new, edit, create, update and destroy. You may use any order you choose, but keep in mind that these are public methods; as mentioned earlier in this guide, they must be placed before any private or protected method in the controller in order to work.
def show
@article = Article.find(params[:id])
end
We use Article.find to find the article were interested in, passing in params[:id] to get the :id parameter from the request. We also use an instance variable (prefixed with @) to hold a reference to the article object. We do this because Rails will pass all instance variables to the view
Define index to display all articles. (controller + view)
Add link in views/welcome/index.html.erb
:
<h1>Hello, Rails!</h1>
<%= link_to 'My Blog', controller: 'articles' %>
Add link to create new article in views/articles/index.html.erb
<%= link_to 'New article', new_article_path %>
Validations Update view Using partials to remove view code duplication between edit and new Partial files are prefixed with an underscore Destroy view
Adding a second model
bin/rails generate model Comment commenter:string body:text article:references