go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  Step 6 -- Save data into database
 
Subject: Step 6 -- Save data into database
Author: Linux
In response to: Create Table for Model
Posted on: 09/13/2017 04:28:18 AM


Back to the controller app/controllers/posts_controller.rb, change the content of action create

class PostsController < ApplicationController
 
  def new
  end

  def create
    @post = Post.new(params[post_params]) 
    @post.save
    redirect_to @post
  end
 
  def show
    @post = Post.find(params[:id])
  end

  private
    def post_params
      params.require(:post).permit(:title, :text)
    end

end


This is going to do:
  • Initialize the model Post with params from its respective attributes
  • @post.save -- Save the model into it corresponding table posts
  • redirect_to @post -- redirect the view to show
  • require().permit() -- allow rights

    Showing the post

    app/views/posts/show.html.erb
    <p>
      <strong>Title:</strong>
      <%= @post.title %>
    </p>
     
    <p>
      <strong>Text:</strong>
      <%= @post.text %>
    </p>
    


     

    > On 09/13/2017 01:26:07 AM Linux wrote:

    Now, you can use a Rails command to run the migration:
    administrator@ubuntu:~/blog$ rails db:migrate
    == 20170912222917 CreatePosts: migrating ======================================
    -- create_table(:posts)
       -> 0.0216s
    == 20170912222917 CreatePosts: migrated (0.0219s) =============================
    
    administrator@ubuntu:~/blog$ rails db:migrate RAILS_ENV=test
    == 20170912222917 CreatePosts: migrating ======================================
    -- create_table(:posts)
       -> 0.0057s
    == 20170912222917 CreatePosts: migrated (0.0060s) =============================
    
    administrator@ubuntu:~/blog$ rails db:migrate RAILS_ENV=production
    == 20170912222917 CreatePosts: migrating ======================================
    -- create_table(:posts)
       -> 0.0056s
    == 20170912222917 CreatePosts: migrated (0.0059s) =============================
    



    You can verify the table by connecting to the database:
    administrator@ubuntu:~/blog$ psql -h localhost -p 5432 -U blog_admin blog_development
    
    Password for user blog_admin: 
    psql (9.6.5, server 9.3.19)
    SSL connection (protocol: TLSv1.2, cipher: DHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
    Type "help" for help.
    
    blog_development=> \dt
                     List of relations
     Schema |         Name         | Type  |   Owner    
    --------+----------------------+-------+------------
     public | ar_internal_metadata | table | blog_admin
     public | posts                | table | blog_admin
     public | schema_migrations    | table | blog_admin
    (3 rows)
    
    blog_development=> \q
    





    References:

  •  


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