|
Step 4 -- Generate the resource's components: controller, actions and views |
|
Subject: Step 4 -- Generate the resource's components: controller, actions and views
Author: Linux
In response to: Step 3 -- Create a new resource
Posted on: 09/12/2017 08:35:40 PM
First generate the controller PostsController:
administrator@ubuntu:~/blog$ rails generate controller Posts
Running via Spring preloader in process 43022
create app/controllers/posts_controller.rb
invoke erb
create app/views/posts
invoke test_unit
create test/controllers/posts_controller_test.rb
invoke helper
create app/helpers/posts_helper.rb
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/posts.coffee
invoke scss
create app/assets/stylesheets/posts.scss
Then, the CRUD actions: new -- to bring a new page with form to create a new post create -- to create a new post based on the parameters from form show -- to display a post index -- to display a list of all posts edit -- to bring a updatable page to edit update -- to update a post based on the parameter from edit destroy -- to delete a post
app/controllers/posts_controller.rb
class PostsController < ApplicationController
def new
end
def create
render plain: params[:post].inspect
end
def show
end
def index
end
def edit
end
def update
end
def destroy
end
end
Finally the corresponding views:
app/views/posts/new.html.erb
<h1>New Post</h1>
<%= form_for :post, url: posts_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 %>
This tells form's action going to url: posts_path which defaults to posts#create
def create
render plain: params[:post].inspect
end
This prints on browser the following:
<ActionController::Parameters {"title"=>"My First Post", "text"=>"This post to tell how to do ..."} permitted: false>
For sure, we need more than just display -- to save the post into a DB storage.
>
> On 09/12/2017 01:55:13 AM Linux wrote:
What is resource? A resource is the term used for a collection of similar objects, such as organization, people or books. You can create, read, update and destroy items for a resource and these operations are referred to as CRUD operations.
Rails provides a resources method which can be used to declare a standard REST resource. For example, you can add a posts resource to the config/routes.rb for your blog.
Rails.application.routes.draw do
get 'welcome/index'
resources :posts
root 'welcome#index'
end
And then run:
administrator@ubuntu:~/blog$ rails routes
Prefix Verb URI Pattern Controller#Action
welcome_index GET /welcome/index(.:format) welcome#index
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
root GET / welcome#index
This shows that you have a complete CRUD actions implicitly ready inside the routing table.
References:
|
|
|
|