go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  Step 5 Create a model
 
Subject: Step 5 Create a model
Author: Linux
In response to: Step 4 -- Generate the resource's components: controller, actions and views
Posted on: 09/13/2017 01:21:04 AM


To create a new model, run this command:

administrator@ubuntu:~/blog$ rails generate model Post title:string text:text

Running via Spring preloader in process 44241
      invoke  active_record
      create    db/migrate/20170912222917_create_posts.rb
      create    app/models/post.rb
      invoke    test_unit
      create      test/models/post_test.rb
      create      test/fixtures/posts.yml


This is going to do:
  • Create a model Post inside file app/models/post.rb
    class Post < ApplicationRecord
    end
    


  • Generate a database instruction file db/migrate/20170912222917_create_posts.rb
    class CreatePosts < ActiveRecord::Migration[5.1]
      def change
        create_table :posts do |t|
          t.string :title
          t.text :text
    
          t.timestamps
        end
      end
    end
    

    to create a table posts with a title column of type string and a text column of type text, together with timestamps

  • Map the model Psot with the database table posts


     

    > On 09/12/2017 08:35:40 PM Linux wrote:

    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.





    References:

  •  


     
    Powered by ForumEasy © 2002-2022, All Rights Reserved. | Privacy Policy | Terms of Use
     
    Get your own forum today. It's easy and free.