rails new <appName>
- Modal -> Database -> validation , query, Modification of data.
- View -> HTML and ERB files / JSON / XML files -> output of application
- Controller -> Handles the request and decides how things are build.
- .ruby-version : decides the ruby version
- config.ru : file for rack, rack -> web framework for ruby. lightweight http processor.
- Gemfile: Gems and dependeincies
- Rakefile : Define the commands which does the task eg. db migrate , rails new ...
- app/assets : Images and stylesheet.
- app/channel: ActionCabel Websockets.
- app/controller : Decides what to do when request comes in.
- app/helpers : some side function
- app/javascript : javascript content goes here.
- app/jobs: background jobs eg. upload
- app/mailers: sending emails
- app/models: for database communication and schema of database.
- view: HTML and other templates goes here.
- bin : it has excutable files
- config/application.rb : we can difine the environments here. eg. dev, prod, test
- config/cabel.yml : configurings websockets,
- config/master.key : environment variables and secret keys
- config/database.yml : connection qith database.
- config/environment.rb : checks the enviroment of application and goes to config/enviroment folder and excute as per requirement.
- config/puma.rb: rails server configuration
- config/routes.rb: defined the route of applications.
- config/storage.yml : define where we can upload the file
- db: this will have migration details
- db/seeds.rb : file used to add some pre-populate some data
- lib : some assets, or some rake rask we can create
- public: error pages, logos, static files
- storage: file upload and other upload detaisl
- test: test cases and automated test case.
- tmp : stores cache data and temporary data
- vendor: eg. if need jQuery, we can put it here.
rails g model <ModelName> fieldName:datatype fieldname:datatype
To migrate table created on rails in to database rails db:migrate
rails console
: this will open the IRB console where we can update the database.
Some commands to update and get data in IRB
To get all blog_posts
- BlogPost.all
create a blogpost with give data
- BlogPost.create(title: "Hello" , body: "the body")
delete blogPost with id 1
- BlogPost.delete(1)
find blogPost with id 2
- BlogPost.find(2)
To update the blogPost :
- blgpst = BlogPost.find(2)
- blgpst.update(title: "this is updated ")
- blgpst.save
To delete it from database
- blgpst.destroy
- root route -> by default it points to /
- rails route are always defined in specific order
- syntax for route:
controller_name#action_name
- Can create different routes for GET, POST, PATCH, PUT and DELETE requests
get '/blog_posts/:id', to: 'blog_post#show'
- Grabing the params from route
@blog_posts = BlogPost.find(params[:id])
- Route generator
and this will generate follwoing function in code
get '/blog_posts/:id', to: 'blog_post#show' , as: :blog_post
blog_post_path(1) -> '/blog_posts/1' blog_post_url(1) -> 'http://localhost:3000/blog_posts/1'
NOTE: everytime we create a file in rails we make sure to create a class in that file which matches the name of that file
- Controllers are inherited from
ApplicationController
, and it is inherrited fromActionController::Base
- It gives a all the procesing thing to us which we can do with HTTP methods.
- Naming conventions and declaration of controller class : eg.
class BlogPostsController
- File Nomenclature: eg. `
- every view is associated with controller. So, in view also we need to create
/blog_posts/index.html.erb
file - Instanct Varible are gloab variable that are declared in controller and can be accessed in a view.
@blog_posts = BlogPost.all
- ERB tags
<% 1 %> -> // is not visible in html <%= 2 %> -> // will be visible to html
- Record not found:
rescue ActiveRecord::RecordNotFound
redirect_to root_path
end
// root_path -> "/"
<%= link_to <text we want to disply> ,'<route we want it to redirect>' %>
eg.
<%= link_to @blog_title ,'/blog_posts/#{@blog_title.id}' %>
we need to route to be added in the form
- POST REQUEST - post "/blog_posts" , to: "blog_posts#create", as: :blog_posts
- GET REQUEST - get "/blog_posts/new", to: "blog_posts#new", as: :new_blog_post
We also need to add functions in controller to work this. And form code can be seen like below.
<%= form_with model: @blog do |form| %>
<div>
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div>
<%= form.label :body %>
<%= form.text_area :body %>
</div>
<%= form.button %>
<% end %>
new
function ins controller
def new
@blog = BlogPost.new
end
- it is private method
params.require(<Parameter>).permit(<list of allowed fields>)
private
def blog_post_allowed_params
params.require(:blog_post).permit(:title, :body)
end
- This action run before running of any function
before_action :[Symbol], only: [Array of action names], except: [Array of actions]
before_action :set_blog_post_by_id, only: [:show, :delete, :update, :edit] , except: [:new]
-
bundle install devise
-
rails g devise:install
-
rails g devise User
-
rails db:migrate
-
devise generate the view and route along with controller, which we can use directly
-
before_action
method is alos provided by device which we can call for authorization. -
before_action :authenticate_user!
Add<NameOfColumn>To<NameOfTable> <column_name>:<format>
rails generate migration AddPublishAtToBlogPosts published_at:datetime