Write your first Ruby On Rails App

In this tutorial we will create a very simple Ruby on Rails web application, we will call it BlocNote.

1. Execute the following commands to generate you app skeleton and database:

$ rails new BlocNote
$ cd Notes
$ bundle install
$ rails generate model note title:string content:string author:string 
$ bundle exec rake db:migrate
$ rails generate controller note index new_note

2. Modify your file /BlocNote/app/controllers/note_controller.rb as the following:

class NoteController < ApplicationController
    def index
        @notes=Note.all
    end
    def new_note
        note=Note.new(:title => params[:title],:content => params[:content],:author => params[:author])
  note.save
  redirect_to "/note/index"
    end
end

2. Modify your file /BlocNote/app/views/note/index.html.erb as the following:

<h1>Note#index</h1>
<p>Find me in app/views/note/index.html.erb</p>
<%= form_tag "new_note" do %>
    <%= text_field_tag "title" %>
    <br>
    <%= text_area_tag "content" %>
    <br>
    <%= text_field_tag "author" %>
    <br>
    <%= submit_tag "Add" %>
<% end %>

<hr>

<% @notes.each do |note| %>
    <h3><%=note.title%></h3>
    <p>
        <%=note.content%>
        <br><b>By: <%=note.author %></b>
    </p>
<%end%>

3. In your /BlocNote/config/routes.rb file modify 
get 'note/new_note'
by
post 'note/new_note' => "now_note"

4. Launch your rails server

$ rails s



No comments:

Post a Comment