Rails Create new Project

 Rails Create New Project


first you create a new project commend is:

 
rails@rails-dell:~/Desktop$ rails new blog


Add the blog folder in file 

 
rails@rails-dell:~/Desktop$ cd blog
rails@rails-dell:~/Desktop/blog$


Go to visual studio code see And see The rails application:-



generate model in rails application:-

 
rails@rails-dell:~/Desktop/blog$ rails g model Message title:
string description:text


See this in the terminal:-


invoke  active_record
      create    db/migrate/20230213145830_create_messages.rb
      create    app/models/message.rb
      invoke    test_unit
      create      test/models/message_test.rb
      create      test/fixtures/messages.yml

Let's create migrate

 
rails@rails-dell:~/Desktop/blog$ rails db:migrate


See this in the terminal:-

== 20230213145830 CreateMessages: migrating =================================== -- create_table(:messages) -> 0.0035s == 20230213145830 CreateMessages: migrated (0.0036s) ==========================
     

 generate controller in rails application:-


 
rails@rails-dell:~/Desktop/blog$ rails g controller Messages


See this in the terminal:-


create app/controllers/messages_controller.rb invoke erb create app/views/messages invoke test_unit create test/controllers/messages_controller_test.rb invoke helper create app/helpers/messages_helper.rb invoke test_unit


Let's start by adding a route to our routes file, config/routes.rb, at the top of the Rails.application.routes.draw block:


Rails.application.routes.draw do

# Defines the root path route ("/")
root "messages#index"
resources :messages
end

go to the application see db/schema.rb


ActiveRecord::Schema[7.0].define(version: 2023_02_13_145830) do
create_table "messages", force: :cascade do |t|
t.string "title"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

The most important of these is the controller file, app/controllers/messages_controller.rb. Let's take a look at it:


class MessagesController < ApplicationController
before_action :find_message, only: [:show,:edit,:update,:destroy]
def index
@message = Message.all
end

def new
@message = Message.new
end

def create
@message = Message.new(message_params)
if @message.save
redirect_to root_path
else
render :new
end
end
def show
end

def update
if @message.update(message_params)
redirect_to message_path(@message)
else
render :edit
end
end
def edit
end
def destroy
@message.destroy
redirect_to "/"
end
private
def message_params
params.require(:message).permit(:title,:description)
end
def find_message
@message = Message.find(params[:id])
end
end



Controller instance variables can be accessed by the view. That means we can reference @messages in app/views/messages/index.html.erb. Let's open that file, and replace its contents with:


<% @messages.each do |ms|%>


<b> Title </b> <%= ms.title %> <br>
<b> Description </b> <%= ms.Description %> <br>
<%= link_to "View Message", message_path(ms) %> <br>

<% end %>
<%= link_to "New Message", new_message_path %>


The show action calls Message.find  with the ID captured by the route parameter. The returned article is stored in the @message instance variable, so it is accessible by the view. By default, the show action will render app/views/messages/show.html.erb.

Let's create app/views/messages/show.html.erb, with the following contents:


<b>Title:</b><%= @message.title %>
<p><b>Description:</b><%= @message.description %></p>
<%= link_to "Edit", edit_message_path(@message)%>
<%= button_to "Delete",message_path(@message), method: :delete,
data: { confirm:"Are you Sure?" } %>
<%= link_to "Back", root_path %>

Using Partials to Share View Code

Our edit form will look the same as our new form. Even the code will be the same, thanks to the Rails form builder and resourceful routing. The form builder automatically configures the form to make the appropriate kind of request, based on whether the model object has been previously saved.

Because the code will be the same, we're going to factor it out into a shared view called a partial. Let's create app/views/messages/_form.html.erb with the following contents:


<%= form_with model: @message do |form| %>

<div>
<%= form.label :Title %>
<%= form.text_field :title %>
</div>

<div>
<%= form.label :Description %>
<%= form.text_area :description %>
</div>

<div>
<%= form.button :submit %>
</div>

<% end %>

The above code is the same as our form in app/views/messages/new.html.erb, except that all occurrences of @article have been replaced with message. Because partials are shared code, it is best practice that they do not depend on specific instance variables set by a controller action. Instead, we will pass the article to the partial as a local variable.

Let's update app/views/messages/new.html.erb to use the partial via


<h1>New Messages</h1>

<%= render "form"%>



And now, let's create a very similar app/views/articles/edit.html.erb:


<h1>Edit Messages</h1>

<%= render "form"%>




Youtube link 

Comments

Popular posts from this blog

what is devise ? and how to use devise gem

Ruby Variable